-1

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(), but that comes up with nothing. Which is wrong, because I use role-specific functionality elsewhere and that works fine.

Following the link in Marco's comment, it mentions using ClaimsIdentity, but for me it says ClaimsIdentity is not declared (Rick left a comment there this past August explaining it's not working for him like something changed in the past 2 years).

I had also tried this, which is similar to code in a controller I have elsewhere which does work:

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

But at runtime I get an error on the "Dim manager" line that says

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.

I have no idea what value it's talking about.

Andy
  • 616
  • 11
  • 32
  • Possible duplicate of [asp.net identity get all roles of logged in user](http://stackoverflow.com/questions/21688928/asp-net-identity-get-all-roles-of-logged-in-user) – Marco Oct 16 '16 at 18:01
  • @Marco - edited to add additional details per your comment – Andy Oct 16 '16 at 19:26
  • Try what is said in the article in my comment. Should work. Use a C#/vb Converter, if you need to. – Marco Oct 16 '16 at 19:42
  • @Marco, are you referring to the tutorial the asker links in their question? That article specifies using "String[] roles = Roles.GetRolesForUser();", which I translated to VB as in my edited question above. It is returning Nothing. – Andy Oct 16 '16 at 20:08
  • no, I meant the answer in the question in my first comment. – Marco Oct 16 '16 at 20:43
  • @Marco - about using ClaimsIdentity? I said in my edited question I tried that and it says ClaimsIdentity is not declared. Is there something else? – Andy Oct 16 '16 at 21:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125871/discussion-between-marco-and-andarta). – Marco Oct 17 '16 at 06:23

1 Answers1

0

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
Andy
  • 616
  • 11
  • 32