适用于
-
Microsoft® Windows® Identity Foundation (WIF)
-
ASP.NET® Web 窗体
总结
本操作说明提供了创建简单的声明感知 ASP.NET Web 窗体应用程序的详细分步过程。 还提供关于如何测试简单声明感知 ASP.NET Web 窗体应用程序以确保成功实现联合身份验证的说明。 本操作说明不包括如何创建安全令牌服务 (STS) 的详细说明,且假定你已配置 STS。
内容
-
目标
-
步骤摘要
-
步骤 1 – 创建简单的 ASP.NET Web 窗体应用程序
-
第 2 步 – 为基于声明的身份验证配置 ASP.NET Web 窗体应用程序
-
步骤 3 - 测试你的解决方案
目标
-
为基于声明的身份验证配置 ASP.NET Web 窗体应用程序
-
测试成功的声明感知 ASP.NET Web 窗体应用程序
步骤摘要
-
步骤 1 – 创建简单的 ASP.NET Web 窗体应用程序
-
步骤 2 – 为联合身份验证配置 ASP.NET Web 窗体应用程序
-
步骤 3 - 测试你的解决方案
步骤 1 – 创建简单的 ASP.NET Web 窗体应用程序
在此步骤中,将创建一个新的 ASP.NET Web 窗体应用程序。
创建一个简单 ASP.NET 应用程序
-
启动 Visual Studio,然后依次单击“文件”、“新建”和“项目”。
-
在“新建项目”窗口中,单击“ASP.NET Web 窗体应用程序”。
-
在“名称”中,输入
TestApp,然后按“确定”。
第 2 步 – 为基于声明的身份验证配置 ASP.NET Web 窗体应用程序
在此步骤中,将配置条目添加到 ASP.NET Web 窗体应用程序的 Web.config 配置文件中,以使其可感知声明。
为基于声明的身份验证配置 ASP.NET Web 应用程序
-
将以下配置节条目紧随添加到 Web.config 配置文件中的 <configuration> 起始元素后:
<configSections> <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" /> </configSections> -
添加 <location> 元素,用于启用对应用程序的联合元数据的访问权限:
<location path="FederationMetadata"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web> </location> -
在 <system.web> 元素内添加以下配置条目以拒绝用户、禁用本机身份验证并启用 WIF 来管理身份验证。
<authorization> <deny users="?" /> </authorization> <authentication mode="None" /> -
添加 <system.webServer> 元素,用于为联合身份验证定义模块。 请注意,PublicKeyToken 特性必须与之前为 <configSections > 条目添加的 PublicKeyToken 特性相同:
<system.webServer> <modules> <add name="WSFederationAuthenticationModule" type="System.IdentityModel.Services.WSFederationAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" /> <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler" /> </modules> </system.webServer> -
添加以下 Windows Identity Foundation 相关配置条目,并确保 ASP.NET 应用程序的 URL 和端口号与 <audienceUris> 条目、<wsFederation> 元素的“realm”特性和 <wsFederation> 元素的“reply”特性中的值相匹配。 还需确保“issuer”值与安全令牌服务 (STS) URL 相适应。
<system.identityModel> <identityConfiguration> <audienceUris> <add value="http://localhost:28503/" /> </audienceUris> <issuerNameRegistry type="System.IdentityModel.Tokens.ConfigurationBasedIssuerNameRegistry, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <trustedIssuers> <add thumbprint="1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234" name="YourSTSName" /> </trustedIssuers> </issuerNameRegistry> <certificateValidation certificateValidationMode="None" /> </identityConfiguration> </system.identityModel> <system.identityModel.services> <federationConfiguration> <cookieHandler requireSsl="true" /> <wsFederation passiveRedirectEnabled="true" issuer="http://localhost:13922/wsFederationSTS/Issue" realm="http://localhost:28503/" reply="http://localhost:28503/" requireHttps="true" /> </federationConfiguration> </system.identityModel.services> -
将引用添加到 xref:System.IdentityModel 程序集。
-
编译此解决方案,确保没有任何错误。
步骤 3 - 测试你的解决方案
在此步骤中,将测试为基于声明的身份验证配置的 ASP.NET Web 窗体应用程序。 若要执行基本测试,需添加用于在安全令牌服务 (STS) 颁发的令牌中显示声明的代码。
为基于声明的身份验证测试 ASP.NET Web 窗体应用程序
-
打开 TestApp 项目下的 Default.aspx 文件,并将现有标记替换为以下标记:
%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html> <head id="Head1" runat="server"> <title></title> </head> <body> <h1><asp:label ID="signedIn" runat="server" /></h1> <asp:label ID="claimType" runat="server" /> <asp:label ID="claimValue" runat="server" /> <asp:label ID="claimValueType" runat="server" /> <asp:label ID="claimSubjectName" runat="server" /> <asp:label ID="claimIssuer" runat="server" /> </body> </html> -
保存 Default.aspx 文件,然后打开名为 Default.aspx.cs 的代码隐藏文件。
NOTE 在解决方案资源管理器中,Default.aspx.cs 文件可能隐藏在 Default.aspx 文件下。 如果 Default.aspx.cs 文件不可见,请单击文件旁边的三角形,展开 Default.aspx。
-
将 Default.aspx.cs 的 Page_Load 方法中的现有代码替换为以下代码:
using System; using System.IdentityModel; using System.Security.Claims; using System.Threading; using System.Web.UI; namespace TestApp { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ClaimsPrincipal claimsPrincipal = Thread.CurrentPrincipal as ClaimsPrincipal; if (claimsPrincipal != null) { signedIn.Text = "You are signed in."; foreach (Claim claim in claimsPrincipal.Claims) { claimType.Text = claim.Type; claimValue.Text = claim.Value; claimValueType.Text = claim.ValueType; claimSubjectName.Text = claim.Subject.Name; claimIssuer.Text = claim.Issuer; } } else { signedIn.Text = "You are not signed in."; } } } } -
保存 Default.aspx.cs,并生成解决方案。
-
按 F5 键运行解决方案。
-
应当会看到在安全令牌服务颁发给你的令牌中显示声明的页面。
历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。
来源与署名:Microsoft 和文档贡献者(原始作者元数据 BrucePerlerMS)。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体、发布器占位标签和导航标记。
