【问题标题】:Extending Azure ACS Claims for MVC Sessions为 MVC 会话扩展 Azure ACS 声明
【发布时间】:2012-09-18 23:44:37
【问题描述】:

我正在开发要在 Azure 中托管的 MVC 4 应用程序,并希望使用他们的 ACS 服务进行身份验证。一旦用户通过身份验证,我将使用生成的索赔详细信息与我的本地记录相关联。在此之后,我想扩展声明集以包括代表本地授权的其他声明,我的应用程序将使用这些声明进行授权决策。我假设我需要替换 Principle,但我不确定在 MVC 中的何处/何时执行此操作,并且希望避免破坏通常在整个会话生命周期中使用的任何身份验证管道。任何人都可以对此有所了解吗?

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 azure acs iprincipal


    【解决方案1】:

    WIF 中用于丰富声明集的扩展点是ClaimsAuthenticationManager

    来自文档:

    声明身份验证管理器提供了一个可扩展点 RP 处理管道,可用于过滤、修改或 将新的声明注入到由一个提出的声明集合中 调用 RP 应用程序之前的 IClaimsPrincipal。

    您还可以在 ACS 中添加规则,以使用您需要的声明来丰富令牌。

    【讨论】:

      【解决方案2】:

      除了@Eugenio Pace 所说的之外,值得注意的是,您可以在IClaimsPrincipal 中添加和删除声明:

      public static void UpdateClaims(IClaimsIdentity identity)
      {
          identity.Claims.Remove(identity.Claims.SingleOrDefault(x => x.ClaimType == ClaimTypes.Name));
          identity.Claims.Remove(identity.Claims.SingleOrDefault(x => x.ClaimType == ClaimTypes.Email));
          identity.Claims.Add(new Claim(ClaimTypes.Name, "Steve Smith"));
          identity.Claims.Add(new Claim(ClaimTypes.Email, "steve@smith.com"));
      }
      
      UpdateClaims(User.Identity as IClaimsIdentity);
      

      添加的声明可以是ClaimTypes 中列举的类型之一,也可以是您自己设计的自定义字符串。您可以添加多个 ClaimTypes.Role 类型的声明 - 我不确定其他类型。

      来自ClaimsCollection 文档:

      表示与单个主题相关联的声明集合。

      将声明添加到 ClaimCollection 会隐式关联该声明 通过调用与集合关联的主题 SetSubject 方法。

      从 ClaimCollection 中移除 Claim 会隐式移除 this 通过调用 SetSubject 方法进行关联。

      http://msdn.microsoft.com/en-us/library/microsoft.identitymodel.claims.claimcollection.aspx

      更新

      对于.Net 4.5,身份类和更新声明的方法发生了变化,命名空间也发生了变化:

      using System.IdentityModel;
      using System.Security.Claims;
      
      public static void UpdateClaims(Member member, ClaimsIdentity identity)
      {
          identity.RemoveClaim(identity.Claims.SingleOrDefault(x => x.Type == ClaimTypes.Name));
          identity.RemoveClaim(identity.Claims.SingleOrDefault(x => x.Type == ClaimTypes.Email));
          identity.AddClaim(new Claim(ClaimTypes.Name, "Steve Smith"));
          identity.AddClaim(new Claim(ClaimTypes.Email, "steve@smith.com"));
      }
      
      UpdateClaims(User.Identity as ClaimsIdentity);
      

      http://msdn.microsoft.com/en-us/library/system.security.claims.claimsidentity.aspx

      【讨论】:

        猜你喜欢
        • 2018-11-17
        • 1970-01-01
        • 2022-10-08
        • 1970-01-01
        • 1970-01-01
        • 2015-10-27
        • 2019-05-18
        • 2016-08-17
        • 1970-01-01
        相关资源
        最近更新 更多