我正在使用 Entity Framework 5 Database First 方法开发 MVC 5 Web 应用程序。我正在使用 OWIN 对用户进行身份验证。下面显示了我的帐户控制器中的登录方法。
public ActionResult Login(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid)
{
var user = _AccountService.VerifyPassword(model.UserName, model.Password, false);
if (user != null)
{
var identity = new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, model.UserName), }, DefaultAuthenticationTypes.ApplicationCookie, ClaimTypes.Name, ClaimTypes.Role);
identity.AddClaim(new Claim(ClaimTypes.Role, "guest"));
identity.AddClaim(new Claim(ClaimTypes.GivenName, "A Person"));
identity.AddClaim(new Claim(ClaimTypes.Sid, user.userID)); //OK to store userID here?
AuthenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent = model.RememberMe
}, identity);
return RedirectToAction("Index", "MyDashboard");
}
else
{
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
如您所见,我正在创建一个 ClaimsIdentity 并向其添加多个声明,然后使用 AuthenticationManager 将其传递给 OWIN 以执行登录。
我遇到的问题是我不确定如何在我的应用程序的其余部分(无论是在控制器中还是在 Razor 视图中)访问声明。
我尝试过本教程中列出的方法
例如,我在我的控制器代码中尝试了这个,试图访问传递给声明的值,但是,user.Claims 等于 null
var ctx = HttpContext.GetOwinContext();
ClaimsPrincipal user = ctx.Authentication.User;
IEnumerable<Claim> claims = user.Claims;
也许我在这里遗漏了一些东西。
更新
根据达林的回答,我添加了他的代码,但我仍然看不到对声明的访问。请参阅下面的屏幕截图,显示我将鼠标悬停在 identity.Claims 上时看到的内容。
https://i.stack.imgur.com/Uk6RQ.jpg
尝试这个:
[Authorize]
public ActionResult SomeAction()
{
var identity = (ClaimsIdentity)User.Identity;
IEnumerable<Claim> claims = identity.Claims;
...
}
你也可以这样做:
//Get the current claims principal
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
var claims = identity.Claims;
更新
根据评论提供进一步的解释。
如果您在系统中创建用户,如下所示:
UserManager<applicationuser> userManager = new UserManager<applicationuser>(new UserStore<applicationuser>(new SecurityContext()));
ClaimsIdentity identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
您应该自动填充一些与您的身份相关的声明。
要在用户进行身份验证后添加自定义声明,您可以执行以下操作:
var user = userManager.Find(userName, password);
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));
可以按照 Darin 上面的回答或我的回答来读出这些声明。
当您在下面调用传递身份时,声明将持续存在:
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = persistCookie }, identity);
我创建了自己的扩展类来查看我需要什么,所以当我需要进入我的控制器或视图时,我只将 using 添加到我的命名空间中,如下所示:
public static class UserExtended
{
public static string GetFullName(this IPrincipal user)
{
var claim = ((ClaimsIdentity)user.Identity).FindFirst(ClaimTypes.Name);
return claim == null ? null : claim.Value;
}
public static string GetAddress(this IPrincipal user)
{
var claim = ((ClaimsIdentity)user.Identity).FindFirst(ClaimTypes.StreetAddress);
return claim == null ? null : claim.Value;
}
public ....
{
.....
}
}
在我的控制器中:
using XXX.CodeHelpers.Extended;
var claimAddress = User.GetAddress();
在我的剃须刀里:
@using DinexWebSeller.CodeHelpers.Extended;
@User.GetFullName()
return claim?.Value;
因为为什么不
如果您不想一直使用声明,这是一种替代方法。看看 Ben Foster 的这个tutorial。
public class AppUser : ClaimsPrincipal
{
public AppUser(ClaimsPrincipal principal)
: base(principal)
{
}
public string Name
{
get
{
return this.FindFirst(ClaimTypes.Name).Value;
}
}
}
然后你可以添加一个基本控制器。
public abstract class AppController : Controller
{
public AppUser CurrentUser
{
get
{
return new AppUser(this.User as ClaimsPrincipal);
}
}
}
在您的控制器中,您将执行以下操作:
public class HomeController : AppController
{
public ActionResult Index()
{
ViewBag.Name = CurrentUser.Name;
return View();
}
}
要进一步了解 Darin 的答案,您可以使用 FindFirst 方法获取您的具体声明:
var identity = (ClaimsIdentity)User.Identity;
var role = identity.FindFirst(ClaimTypes.Role).Value;
你也可以这样做。
IEnumerable<Claim> claims = ClaimsPrincipal.Current.Claims;
请记住,为了查询 IEnumerable,您需要参考 system.linq。它将为您提供所需的扩展对象:
CaimsList.FirstOrDefault(x=>x.Type =="variableName").toString();
@Rosdi Kasim'd 答案的最短和简化版本是
string claimvalue = ((System.Security.Claims.ClaimsIdentity)User.Identity).
FindFirst("claimname").Value;
Claimname
是您要检索的声明,即如果您正在寻找“StreedAddress”声明,那么上述答案将是这样的
string claimvalue = ((System.Security.Claims.ClaimsIdentity)User.Identity).
FindFirst("StreedAddress").Value;
Request.GetOwinContext().Authentication.User.Claims
但是,最好在“GenerateUserIdentityAsync”方法中添加声明,特别是如果启用了 Startup.Auth.cs 中的 regenerateIdentity。
GenerateUserIdentityAsync
是一个很棒的建议,我完全忽略了它。非常感谢罗勒。
根据 ControllerBase 类,您可以获得执行操作的用户的声明。
https://i.stack.imgur.com/NNXKa.png
这是您可以在 1 行中完成的方法。
var claims = User.Claims.ToList();
var claim = User.Claims.FirstOrDefault(c => c.Type == "claim type here");
我在我的基本控制器中使用它。只是分享准备使用。
public string GetCurrentUserEmail() {
var identity = (ClaimsIdentity)User.Identity;
IEnumerable<Claim> claims = identity.Claims;
var email = claims.Where(c => c.Type == ClaimTypes.Email).ToList();
return email[0].Value.ToString();
}
public string GetCurrentUserRole()
{
var identity = (ClaimsIdentity)User.Identity;
IEnumerable<Claim> claims = identity.Claims;
var role = claims.Where(c => c.Type == ClaimTypes.Role).ToList();
return role[0].Value.ToString();
}