首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >MVC 5 Access声明身份用户数据

MVC 5 Access声明身份用户数据
EN

Stack Overflow用户
提问于 2014-01-28 19:53:06
回答 11查看 215.7K关注 0票数 127

我正在使用Entity Framework5数据库优先方法开发一个MVC5 web应用程序。我使用中的OWIN对用户进行身份验证。下面显示了我在my Account Controller中的登录方法。

代码语言:javascript
复制
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以执行登录。

我遇到的问题是,我不确定如何在应用程序的其余部分中访问声明,无论是在控制器中还是在视图中。

我已经尝试过本教程中列出的方法

http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/

例如,我在控制器代码中尝试这样做,试图访问传递到声明中的值,但是,user.Claims等于空

代码语言:javascript
复制
var ctx = HttpContext.GetOwinContext();
ClaimsPrincipal user = ctx.Authentication.User;
IEnumerable<Claim> claims = user.Claims;

也许我在这里遗漏了什么。

更新

根据Darin的回答,我添加了他的代码,但我仍然看不到对声明的访问。请看下面的屏幕截图,它显示了我在identity.Claims上悬停时看到的内容。

EN

回答 11

Stack Overflow用户

回答已采纳

发布于 2014-01-28 20:02:31

试试这个:

代码语言:javascript
复制
[Authorize]
public ActionResult SomeAction()
{
    var identity = (ClaimsIdentity)User.Identity;
    IEnumerable<Claim> claims = identity.Claims;
    ...
}
票数 184
EN

Stack Overflow用户

发布于 2014-01-28 20:09:46

您还可以执行以下操作:

代码语言:javascript
复制
//Get the current claims principal
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
var claims = identity.Claims;

更新

根据评论提供进一步的解释。

如果要在系统中创建用户,请执行以下操作:

代码语言:javascript
复制
UserManager<applicationuser> userManager = new UserManager<applicationuser>(new UserStore<applicationuser>(new SecurityContext()));
ClaimsIdentity identity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);

您应该自动填充一些与您的身份相关的声明。

要在用户身份验证后添加自定义声明,您可以执行以下操作:

代码语言:javascript
复制
var user = userManager.Find(userName, password);
identity.AddClaim(new Claim(ClaimTypes.Email, user.Email));

这些声明可以按照Darin上面的回答或我的回答重新读出。

当您在下面的调用中传递身份时,声明将持久化:

代码语言:javascript
复制
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = persistCookie }, identity);
票数 36
EN

Stack Overflow用户

发布于 2015-04-15 07:23:18

我创建了自己的扩展类来查看我需要的东西,所以当我需要进入我的控制器或视图时,我只需要将see添加到我的名称空间中,如下所示:

代码语言:javascript
复制
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 ....
    {
      .....
    }
}

在我的控制器中:

代码语言:javascript
复制
using XXX.CodeHelpers.Extended;

var claimAddress = User.GetAddress();

在我的剃刀里:

代码语言:javascript
复制
@using DinexWebSeller.CodeHelpers.Extended;

@User.GetFullName()
票数 33
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21404935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档