@html.antiforgerytoken() 原理,Html.AntiForgeryToken只有IIdentity

我有一个实现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 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)?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值