I am using MVC Entity Framework and I need to get a user's role within the _LoginPartial for some role-specific functionality within the nav bar. What is the best way to get that there?
I've tried using
Dim myRoles = Roles.GetRolesForUser()
Dim context As IOwinContext = New OwinContext
Dim manager = New AppUserManager(New UserStore(Of AppUser(context.Get(Of MyAppIdentityDbContext)()))
Dim userInfo = manager.FindById(curUserID)
Dim userRole As String = userInfo.Roles(0).RoleId
myRole = db.Roles.Where(Function(x) x.Id = userRole).FirstOrDefault().Name
An exception of type 'System.ArgumentNullException' occurred in
Microsoft.AspNet.Identity.EntityFramework.dll but was not handled in
user code Additional information: Value cannot be null.
Thanks to Marco's help and the other question he linked, I got it using the following code:
@Imports Microsoft.AspNet.Identity @Imports System.Security.Claims @Code Dim db = New MyAppIdentityDbContext Dim curUserID = User.Identity.GetUserId() Dim myFirstName As String = (From users In db.Users Where users.Id = curUserID Select users.FirstName).FirstOrDefault Dim myRole As String = "" If curUserID IsNot Nothing AndAlso curUserID <> "" Then Dim userID = CType(User.Identity, ClaimsIdentity) Dim claims = userID.Claims Dim roleType = userID.RoleClaimType Dim myRoles = claims.Where(Function(c) c.Type = roleType).ToList() myRole = (myRoles.FirstOrDefault.ToString) 'Here myRole contains whole https string - need to strip it to actual value Dim lastInd = myRole.LastIndexOf(" ") myRole = myRole.Substring(lastInd + 1, myRole.Length - (lastInd + 1)) End If End Code