ASP.NET Core - Add role claim to User

前端 未结 3 678
慢半拍i
慢半拍i 2020-12-24 03:47

I\'ve an ASP.NET Core (based on .NET Framework) using Windows Authentication. Point is, I need to add a role claim on that user and this role is stored in a distant database

3条回答
  •  盖世英雄少女心
    2020-12-24 04:19

    Well beside the answers, I just found the answer which is totally predefined in asp .net core. When you are adding claims just :

    var claims = new List
    {
        new Claim(ClaimTypes.Name, UserName),
        new Claim(ClaimTypes.Role, "User"),
        new Claim(ClaimTypes.Role, "Admin"),
        new Claim(ClaimTypes.Role, Watever)
    };
    

    after that you can just use it as said:

    [Authorize(Roles = "Watever")]
    

    or

    User.IsInRole("Watever")
    

提交回复
热议问题