asp.net mvc - Authentication & Authorization Role issue -


i'm having controller name usercontroller in admin area (section). in can assign them roles admin (a) user (u) or user (u) admin (a).

this image of list of users can assign them roles

when change role of user updated in database ,but when login application user of had changed role user contains old role.i have put break point variable 'role' returning previous role. i'm surprised how can 'role' variable return old role.

public override string[] getrolesforuser(string username)         {         string[] role = { obj.getall().where(x => x.emailaddress ==    username).firstordefault().role };         return role;     } 

user controller assignrole action in code i'm updating role

 public actionresult assignrole(int id, string role)     {         try         {         bol.tbl_login user = (bol.tbl_login)obj.login.getbyid(id);         if (role == "a")         {             user.role = "u";         }         else if (role == "u")         {             user.role = "a";         }         else         {             user.role = "u";         }         obj.login.update(user);         tempdata["msg"] = "operation successfully!";          }         catch (exception ex)         {             tempdata["msg"] = "error " + ex.message;         }         return redirecttoaction("_index");     } 

and i'm using

   [authorize(roles = "a")] 

i think entity framework not returning newest data.

here data access layer code

public override void update(t data)     {         obj.entry(data).state = entitystate.modified;         obj.configuration.validateonsaveenabled = false;         save();          obj.configuration.validateonsaveenabled = true;     } 

your problem role saving both previous , current values. asnotracking function not save previous values shows current value. put code there getting roles or above method 'getroles' problem solved

    public override ienumerable<t> getall()     {          return obj.set<t>().asnotracking().tolist();     } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -