Html.AntiForgeryToken只有IIdentity

时间:2016-03-21 18:53:18

标签: asp.net-mvc wif

我有一个实现IIdentity的自定义标识,但它不会从ClaimsIdentity继承。在过去,只要它实现了IIdentity,您就可以拥有一个可以正常运行的自定义标识。

我已将ClaimsAuthenticationManager用于将外部SSO进程映射到我的自定义标识。由于每个提供程序都可以呈现不同的声明集,因此我使用ClaimsAuthenticationManager将它们映射到适当的离散属性。提供商不提供任何权限。所有权限都在应用程序数据库中管理。即,我只利用外部身份验证,但授权是通过我自己的数据库管理的。

问题是AntiForgeryConfig.UniqueClaimTypeIdentifier似乎要求我的自定义标识继承自ClaimsIdentity。我没有看到ClaimsIdentity从IIdentity以外继承的任何接口。这意味着我可以告诉我唯一的选择是从ClaimsIdentity继承,并且它继承了很多其他方法/属性,我对我的自定义标识中没有任何兴趣。只是为了支持

换句话说,当使用这样的身份时:

public class CustomIdentity : IIdentity
{

    public int UserId { get; set; }
    public string Name { get; set; }
    public string AuthenticationType { get; set; }
    public bool IsAuthenticated { get; set; }
    public int SomeCustomProperty { get;set;}
    public List<CustomPermissions> Permissions {get;set;}
}

我收到错误:

  

类型索赔   'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier'   要么   'http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider'   未提供的ClaimsIdentity。启用防伪   令牌支持与基于声明的身份验证,请验证   配置的索赔提供商正在提供这两项索赔   它生成的ClaimsIdentity实例。如果配置了声明   提供者使用不同的声明类型作为唯一标识符,   它可以通过设置静态属性来配置   AntiForgeryConfig.UniqueClaimTypeIdentifier。

为了进一步展示我想要实现的目标,这是将基于声明的身份验证映射到我的CustomIdentity(非声明身份):

public class KentorAuthenticationManager : ClaimsAuthenticationManager
{
    public override ClaimsPrincipal Authenticate(string resourceName, ClaimsPrincipal incomingPrincipal)
    {

        ClaimsIdentity claimsIdentity = (ClaimsIdentity)incomingPrincipal.Identity;

        // create our custom identity
        var customIdentity = new CustomIdentity();
        customIdentity.IsAuthenticated = claimsIdentity.IsAuthenticated;
        customIdentity.AuthenticationType = claimsIdentity.AuthenticationType;
        customIdentity.Name = claimsIdentity.Name;

        // ...if IsAuthenticated, use SAML NameId to 
        //        find user with matching AuthenticationUserExternalId in our database
        // ...initialize CustomIdentity.Permissions from database

        GenericPrincipal newPrincipal = new GenericPrincipal(customIdentity, null);          
        return newPrincipal;

    }

}

使用此方法,身份验证完美无缺。看起来AntiForgeryTokens似乎不应该依赖于具体的ClaimsIdentity实现。强迫我使用如此沉重的身份似乎是荒谬的。

是否可以在没有Html.AntiForgeryToken的情况下使用ClaimsIdentity(同时仅为初始身份验证使用基于声明的身份提供程序)?

0 个答案:

没有答案