我有一个实现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实现 . 强迫我使用如此沉重的身份似乎是荒谬的 .

Is there a way to use Html.AntiForgeryToken without having a ClaimsIdentity (while using a claims based identity provider only for the initial authentication)?