I have a simple role provider I created. (so forms authentication).
The standard built in asp.net logon controls when placed on a page work fine.
I am trying to determine why the standard 'tests' for role memebership seen to fail.
I have this so far:
Roles.Provider.IsUserInRole(Membership.GetUser.UserName, "Portal") returns true
Roles.IsUserInRole(Membership.GetUser.UserName, "Portal") returns false
Why does the second format fail? (note that the below IsInRole code is called by BOTH of the above!!).
User.IsInRole("Portal") returns false.
So, out of the 3 examples, only one works?
Anyone have a suggestion as to why User.IsInRole() does not work?
Note that User.IsInRole() for some reason does NOT call the following routine, but the above TWO codes do.
Public Overrides Function IsUserInRole(username As String, roleName As String) As Boolean
Dim rst As DataTable
Dim strSql As String
MsgBox("Is user in Role test")
strSql = "SELECT Email, RoleName FROM dbo_ContactName " & _
"LEFT JOIN Web_UsersInRoles ON Web_usersInRoles.User_ID = dbo_ContactName.Id " & _
"LEFT JOIN Web_Roles on Web_Roles.ID = Web_UsersInRoles.Role_ID " & _
"WHERE Email = '" & username & "' and RoleName = '" & roleName & "'"
rst = Myrst(strSql)
If rst.Rows.Count > 0 Then
Return True
Else
Return False
End If
End Function
Note that MOST confusing is this:
Roles.IsUserInRole(Membership.GetUser.UserName, "Portal") returns false
I have verified that the above causes my custom code to run but ALWAYS returns false.
In fact if I HARD CODE the above routine to return "true", then the above #2 STILL calls my code and STILL returns false!!!
So big question: Even with hard coding the above routine to always return true why does the this format fail?
Roles.IsUserInRole(Membership.GetUser.UserName, "Portal")
And worse, why does this fail?
User.IsInRole("Portal") again always returns false.
So what settings should I look for to enable 2 and 3 to work?
In summary:
1 - Roles.Provider.IsUserInRole(Membership.GetUser.UserName, "Portal") returns true
2 - Roles.IsUserInRole(Membership.GetUser.UserName, "Portal") returns false
3 - User.IsInRole("Portal") returns false
4 - User.Identity.Name returns the correct logged in user kallal@msn.com
I can also verify that from above #3 NEVER calls the IsUserInRole code. This includes right after a clear browser cache.
A quick BinGoolge shows large number of people find that User.IsInRole() stops working when adopting a custom role provider. There must some be "significant" issue here that being missed when writing custom role providers that causes this to fail.
Links or suggestions on how to get above 2 or 3 options working much appreciated.
Using Visual Studio 2013,and creating a standard asp.net web site with vb.net (frame work 4.5)
Edit: As a follow up ALL OF MY ROLE functions works.
eg: Roles.GetAllRoles() - this works (returns all roles correct)
Roles.GetRolesForUser() - this works, retruns all roles for CURRENT user
Roles.GetUserInRole("Portal") - this works, returns all users in role group "portal"
NOT working:
User.IsInRole("Portal") - as noted 1000's of posts on internet have this issue!
Roles.IsUserInRole(Membership.GetUser.UserName, "Portal")
Here are the additional subs that I had not included in origal post:
Public Overrides Function GetUsersInRole(roleName As String) As String()
Dim rst As DataTable
Dim i As Integer
Dim a As New List(Of String)
Dim strSql As String
MsgBox("get users in role code")
strSql = "SELECT Email, RoleName FROM dbo_ContactName " & _
"LEFT JOIN Web_UsersInRoles ON Web_usersInRoles.User_ID = dbo_ContactName.Id " & _
"LEFT JOIN Web_Roles on Web_Roles.ID = Web_UsersInRoles.Role_ID " & _
"WHERE RoleName = '" & roleName & "' " & _
"ORDER BY Web_Roles.RoleName"
rst = Myrst(strSql)
For i = 0 To rst.Rows.Count - 1
a.Add(rst.Rows(i).ItemArray(0).ToString)
Next
Return a.ToArray
End Function
Public Overrides Function GetRolesForUser(username As String) As String()
Dim rst As DataTable
Dim i As Integer
Dim a As New List(Of String)
Dim strSql As String
strSql = "SELECT Email, RoleName FROM dbo_ContactName " & _
"LEFT JOIN Web_UsersInRoles ON Web_usersInRoles.User_ID = dbo_ContactName.Id " & _
"LEFT JOIN Web_Roles on Web_Roles.ID = Web_UsersInRoles.Role_ID " & _
"WHERE Email = '" & username & "' " & _
"ORDER BY Web_Roles.RoleName"
rst = Myrst(strSql)
For i = 0 To rst.Rows.Count - 1
a.Add(rst.Rows(i).ItemArray(1).ToString)
Next
Return a.ToArray
End Function
Public Overrides Function GetAllRoles() As String()
Dim rst As DataTable
Dim i As Integer
Dim a As New List(Of String)
rst = Myrst("select RoleName from Web_Roles order by RoleName")
For i = 0 To rst.Rows.Count - 1
a.Add(rst.Rows(i).ItemArray(0).ToString)
Next
Return a.ToArray
End Function
ALL OF THE ABOVE features work.
Question still stands:
How do we wire up to ENABLE use of User.IsInRole(). Thiis does NOT work.
So in summary:
Roles.GetRolesForUser - returns "current" roles for user - works fine
Roles.GetUsersInRole("Portal") - works fine
Roles.GetAllRoles() - works fine.
User.IsInRole() - BROKEN!
More summary: here is some code with the resulting output:
Debug.WriteLine(Roles.Provider.IsUserInRole(Membership.GetUser.UserName, "Portal"))
Debug.WriteLine(Roles.IsUserInRole(Membership.GetUser.UserName, "Portal"))
Debug.WriteLine(User.Identity.Name)
Debug.WriteLine(User.Identity.IsAuthenticated)
Debug.WriteLine(User.IsInRole("Portal"))
Output:
Roles.Provider.IsUserInRole = True
Roles.IsUserInRole = False
User.Identity.Name = kallal@msn.com
User.Identity.IsAuthenticated = True
User.IsInRole("Portal") = False < -- still broken!!!