【问题标题】:How to implement External Authentication with local claims in mvc 5如何在 mvc 5 中使用本地声明实现外部身份验证
【发布时间】:2016-05-05 21:35:57
【问题描述】:
我正在开发一个 mvc 5 应用程序,并已将 WS Federation 配置为使用外部 IP 进行身份验证。然而基于应用程序的功能,用户的角色需要由应用程序本身来管理。应用程序的管理员将允许从应用程序的管理区域对应用程序进行不同级别的访问。因为我是第一次使用 ASP.NET Identity,所以我不知道如何从这里继续前进。
- 我需要为此实现自定义 ClaimsAuthenticationManager 吗?
- 我是否还需要实施 UserManager 来创建本地用户列表才能管理他们的声明?
感谢任何方向/帮助。
谢谢,
【问题讨论】:
标签:
asp.net
asp.net-mvc
claims-based-identity
ws-federation
【解决方案1】:
您可以在覆盖 CreateUserIdentityAsync 时添加外部声明。
这将只添加来自您的联合登录的角色:
public override async Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
var externalIdentity =
await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
var localIdentity = await user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);
foreach (var item in externalIdentity.Claims.Where(x => x.Type == ClaimTypes.Role))
localIdentity.AddClaim(new Claim(ClaimTypes.Role, item.Value));
return localIdentity;
}