净核心自定义用户属性 [英] Net core custom user property

查看:49
本文介绍了净核心自定义用户属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET Core项目中使用默认授权.我想检查用户是否为管理员,因此在ApplicationUser.cs模型中添加了以下内容:

I'm using the default authorization in my .NET Core project. I want to check if an user is admin so in ApplicationUser.cs model I've added this:

 public class ApplicationUser : IdentityUser
{

    public bool admin { get; set; }
}

我将这些更改迁移到了我的SQL数据库中,并且可以在AspNetUsers表中看到'admin'属性.如何检查当前登录用户的'admin'属性?

I migrated these changes into my SQL database and I can see the 'admin' property inside the AspNetUsers table. How do I check the 'admin' property of the currently logged in user?

推荐答案

您可以通过UserManager<T>对象访问当前用户实例,该对象可以作为构造函数参数注入到您的控制器中.方法GetUserAsync接受一个ClaimsPrincipal,在这种情况下,它是HttpContext的用户.

You can access the current user instance through the UserManager<T> object, which can be injected into your controller as a constructor parameter. The method GetUserAsync takes a ClaimsPrincipal, which in this case is the User of the HttpContext.

private readonly UserManager<ApplicationUser> _userManager;
public HomeController(UserManager<ApplicationUser> userManager) {
    _userManager = userManager;

    var user = _userManager.GetUserAsync(HttpContext.User);
}

这篇关于净核心自定义用户属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆