asp.net identity get all roles of logged in user

前端 未结 5 2187
遇见更好的自我
遇见更好的自我 2020-11-28 04:49

I created a role based menu for which I followed this tutorial. Some where down that page you\'ll see this line of code:

String[] roles = Roles.GetRolesForU         


        
5条回答
  •  半阙折子戏
    2020-11-28 05:33

    Don't use @using System.IdentityModel.Claims namespace, Instead of that use

    @using System.Security.Claims

        @using System.Security.Claims
        @using Microsoft.AspNet.Identity
        @{      
           var claimsIdentity = User.Identity as System.Security.Claims.ClaimsIdentity;
           var customUserClaim = claimsIdentity != null ? claimsIdentity.Claims.FirstOrDefault(x => x.Type == "cutomType") : null;
           var customTypeValue= customUserClaim != null ? customUserClaim .Value : User.Identity.GetUserName();
           var roleOfUser = claimsIdentity != null ? claimsIdentity.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role).Value :"User";
    
    }
    

提交回复
热议问题