简体   繁体   English

令牌认证-设置自定义用户属性

[英]Token Authentication- Set custom User Property

I want to achieve something like described here , but i have 2 problems: on the constructor of my controller, both HttpContext and therefore, User , are null, and i can't seem to get where that UserManager<T> class... 我想实现类似于此处所述的内容,但是我有两个问题:在控制器的构造函数上, HttpContext以及User均为null,并且我似乎无法获得该UserManager<T>类的位置...

On my controllers action i can get the User and HttpContext , but i don't want to handle Claims conversion case-by-case! 在我的控制器操作上,我可以获取UserHttpContext ,但是我不想逐案处理Claims转换! I would like to create a "BaseController", there have a "MyExtendedUserPrincipal", and on my actions only read stuff from it... 我想创建一个“ BaseController”,有一个“ MyExtendedUserPrincipal”,并且在我的操作中仅从中读取内容...

I'm not using the regular SQL user management middleware... i think thats why i can't get a hold of the UserManager<T> 我没有使用常规的SQL用户管理中间件...我认为这就是为什么我无法获得UserManager<T>

The UserManager<T> class doesn't come out of the box, you have to define it yourself. UserManager<T>类并不是开箱即用的,您必须自己定义它。 You can either use the default implementation, or define your own class if you need to. 您可以使用默认实现,也可以根据需要定义自己的类。

For example: 例如:

MyUserStore.cs MyUserStore.cs

This is where the users come from (eg a DB), and where you can retrieve your own user from any claim from the ClaimsPrincipal . 这是用户来自(例如,数据库)的地方,您可以从ClaimsPrincipal任何索赔中检索自己的用户。

public class MyUserStore: IUserStore<MyUser>, IQueryableUserStore<MyUser>
{
    // critical method to bridge between HttpContext.User and your MyUser class        
    public async Task<MyUser> FindByIdAsync(string userId, CancellationToken cancellationToken)
    {
        // that userId comes from the ClaimsPrincipal (HttpContext.User)
        var user = _users.Find(userId);
        return await Task.FromResult(user);
    }
}

Startup.cs Startup.cs

public void ConfigureServices(IServiceCollection services)        
{
    // you'll need both a user and a role class. You can also implement a RoleStore if needed
    services
        .AddIdentity<MyUser, MyRole>()
        .AddUserStore<MyUserStore>();

    services.Configure<IdentityOptions>(options =>
    {
        // This claim will be used as userId in MyUserStore.FindByIdAsync
        options.ClaimsIdentity.UserIdClaimType = ClaimTypes.Name;
    });
}

MyController .cs MyController .cs

Then, in your controller, you can access the UserManager<MyUser> class: 然后,在您的控制器中,您可以访问UserManager<MyUser>类:

public class MyController : Controller
{
    private readonly UserManager<User> _userManager;
    public MyController(UserManager<User> userManager)
    {
        _userManager = userManager;
    }


    [HttpGet("whatever")]
    public async Task<IActionResult> GetWhatever()
    {
        // this will get your user from the UserStore, 
        // based on the ClaimsIdentity.UserIdClaimType from the ClaimsPrincipal
        MyUser myUser = await _userManager.GetUserAsync(User);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM