asp.net - Roles.GetRolesForUser() in Layout view not returning roles -


@roles.getrolesforuser() in razor layout view not returning roles. @roles.getrolesforuser().count() 0.

while @roles.isuserinrole('name_of_logged_in_role') returns true in same view @ same place.

razor view:

<p>     @user.identity.name //output: myname     @roles.getrolesforuser().count()  //output: 0     @roles.isuserinrole("radiologist")  //output: true </p> 

update

@roles.getrolesforuser(user.identity.name).length //output: 0 @roles.getrolesforuser(user.identity.getusername()).length //output: 0 

after extensive research, found problem. able reproduce issue in web application. apparently, cannot combine asp.net identity simple membership, did getrolesforuser method. roles object default setup simple membership using default provider, seems using asp.net identity not simple membership. didn't notice difference until wondering myself why wasnt working.

the reason why got string[0] because getrolesforuser executed sql query table doesnt exist in database.
reason why isuserinrole worked more or less did not use default provider check used cacherolesincookie

if cacherolesincookie true, rolename may checked against roles cache rather specified role provider. 

so, technically went connectionstring listed default provider , return string[0] because have no data in database connectionstring. adding current database providers not either because simple membership database schema different asp.net identity

that being said, should roles username this:

simple solution:

public list<string> getroles(string username) {    list<string> roles = new list<string>();     if (!string.isnullorwhitespace(username))     {         applicationuser user = context.users.where(u => u.username.equals(username, stringcomparison.currentcultureignorecase)).firstordefault();         var account = new accountcontroller();          roles = account.usermanager.getroles(user.id);              }                 return roles; } 

updated

extended solution:

you extend asp.net identity roles in context

http://www.codeproject.com/articles/799571/asp-net-mvc-extending-asp-net-identity-roles


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -