我已经实现了Rolemanager,如下所述: http://johnatten.com/2014/10/26/asp-net-web-api-and-identity-2-0-customizing-identity-models-and-implementing-role-based-authorization/
现在我需要API将角色从webAPI返回到“客户端”(这是另一个充当前端的ASP.net站点)所以我可以看到我应该向非管理员用户隐藏哪些视图。< / p>
我可以将这些角色添加到Accounts控制器中的GetUserInfo方法,还是应该以不同的方式进行,我应该如何实现呢?
答案 0 :(得分:0)
我最终在AccountController中实现了它:
public UserInfoViewModel GetUserInfo()
{
ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);
var rols = ((ClaimsIdentity) User.Identity).Claims
.Where(c => c.Type == ClaimTypes.Role)
.Select(c => c.Value);
var UIM = new UserInfoViewModel
{
Email = User.Identity.GetUserName(),
HasRegistered = externalLogin == null,
LoginProvider = externalLogin != null ? externalLogin.LoginProvider : null,
roles = rols
};
return UIM;
}
然后将其添加到UserInfoViewModel:
public IEnumerable<string> roles;