WCF 安全令牌服务

在 Visual Studio 中,打开“文件”菜单,然后选择“新建”、“网站”。 选择“WCF 安全令牌服务”。

如果查看 web.config 文件,将会看到它与典型 ASP.NET 网站的 web.config 有多处不同。

  • 添加了以下应用程序设置:

    <appSettings> <add key="IssuerName" value="PassiveSigninSTS"/> <add key="SigningCertificateName" value="CN=STSTestCert"/> <add key="EncryptingCertificateName" value=""/> </appSettings>
    

    STS 使用默认证书来签署其颁发的令牌。 此证书的名称为“STSTestCert”,系统已自动将该证书添加到您的证书存储区以供 STS 使用。 该证书文件存在于 STS 项目中。 该文件的密码为“STSTest”。 在生产练习中,不应使用此密码。 您可以将默认证书替换为任何其他证书。 请确保 IIS 进程的用户有权访问任何此类证书的私钥。 还可以选择创建从 IssuerNameRegistry 派生的类型,以通过编程方式对受信任颁发者的证书进行验证。

  • 为所有用户授予了访问联合元数据的权限。 联合元数据包含有关令牌签名证书公钥、STS 公开的终结点以及发出的声明。

    <location path="FederationMetadata"> <system.web> <authorization> <allow users="*" /> </authorization> </system.web> </location>
    
  • <system.Web>/<assemblies> 元素现在包含对 Microsoft.IdentityModel.dll 程序集的引用:

    <add assembly="Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    
  • 该身份验证已经从“Windows”更改为“None”:

    <authentication mode="None">
    
  • 已添加身份验证服务、配置文件服务和角色服务:

    <system.web.extensions> <scripting> <webServices> <!-- Uncomment this section to enable the authentication service. Include requireSSL="true" if appropriate. <authenticationService enabled="true" requireSSL = "true|false"/> --> <!-- Uncomment these lines to enable the profile service, and to choose the profile properties that can be retrieved and modified in ASP.NET AJAX applications. <profileService enabled="true" readAccessProperties="propertyname1,propertyname2" writeAccessProperties="propertyname1,propertyname2" /> --> <!-- Uncomment this section to enable the role service. <roleService enabled="true"/> --> </webServices> <!-- <scriptResourceHandler enableCompression="true" enableCaching="true" /> --> </scripting> </system.web.extensions>
    
  • 添加了以下服务、终结点、绑定和行为:

    <system.serviceModel> <services> <service name="Microsoft.IdentityModel.Protocols.WSTrust.WSTrustServiceContract" behaviorConfiguration="ServiceBehavior"> <endpoint address="IWSTrust13" binding="ws2007HttpBinding" contract="Microsoft.IdentityModel.Protocols.WSTrust.IWSTrust13SyncContract"  bindingConfiguration="ws2007HttpBindingConfiguration"/> <host> <baseAddresses> <add baseAddress="https://localhost/STSService1/Service.svc" /> </baseAddresses> </host> <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /> </service> </services> <bindings> <ws2007HttpBinding> <binding name="ws2007HttpBindingConfiguration"> <security mode="Message"> <message establishSecurityContext="false" /> </security> </binding> </ws2007HttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> <serviceMetadata httpGetEnabled="true" /> <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors> </behaviors> </system.serviceModel>
    
  • 已添加一个跟踪,可以将其取消注释以启用跟踪功能。 有关详细信息,请参阅 WIF 跟踪如何启用跟踪

    <!-- Uncomment the lines below to enable WIF tracing to: WIFTrace.e2e. Open the trace file using the SvcTraceViewer.exe tool (shipped with the WCF SDK available from Microsoft) or a xml viewer. Refer to MSDN if you wish to add WCF tracing. -->
    
      <!--<system.diagnostics> <sources> <source name="Microsoft.IdentityModel" switchValue="Verbose"> <listeners> <add name="xml" type="System.Diagnostics.XmlWriterTraceListener" initializeData="WIFTrace.e2e" /> </listeners> </source> </sources> <trace autoflush="true" /> </system.diagnostics>-->
    

在 App_Code 文件夹中,打开 CustomSecurityTokenService.cs。

  • 更新 static readonly string[] ActiveClaimsAwareApps 以包括您希望此 STS 向其颁发令牌的信赖方应用程序的 URL。

  • 重写 GetOutputClaimsIdentity 方法时,请添加信赖方应用程序要求 STS 发出的声明,以及您希望 STS 发出的任何自定义声明。

CustomSecurityTokenService.cs 实现以下所需方法。

  1. GetScope。此方法获取调用者的 IClaimsPrincipal 和传入的 RST,并返回令牌颁发请求的配置,由 Scope 类表示。 在此方法中,可以标准化信赖方的地址并选择签署和加密密钥。 通常,对安全令牌进行加密,以便只有信赖方能够读取这些令牌。

  2. GetOutputClaimsIdentity。此方法获取调用者的 IClaimsPrincipal、传入的 RST 和从 GetScope 返回的 Scope 对象,并返回要包括在所颁发令牌中的 IClaimsIdentity。 可通过此方法决定令牌中包括哪些声明。