Update ASP.NET Provider Role

simple question...

Given that I have an ASP.NET site that uses [custom] RoleProvider, is
there a way in which I can somehow "update" the provider without forcing the user to log out and log back in?

I'm looking for something that would look like a fictional method

Roles.Refresh()

In particular, I consider this if the administrator changes the roles of the user, user sessions can be updated every 10 minutes or something like that.

+3
source share
5 answers

I assume you have something like this in web.config:

<roleManager enabled="true" defaultProvider="..." 
             cacheRolesInCookie="true">

cookie, cookie. . cookieName, asp.net default. cookieTimeout - .

, . cookie.

+4

cookie:

#: Roles.DeleteCookie();// Roles.Refresh()

+3

cookie, Session . :

        public override string[] GetRolesForUser(string username)
    {
        System.Web.SessionState.HttpSessionState Session = HttpContext.Current.Session;
        if (Session["roles"] == null)
                Session["roles"] = MyDataProvider.Security.GetRolesForUser(username);
        return (string[])Session["roles"];
    }

,

Session["roles"] = null
+1

.

" " ? ( , , , )

0

cookie (, ). - web.config. .

- cookie. , cookie .

, :

1) aspx

//

Roles.AddUserToRole(User.Identity.Name, Constants.ROLE_REVISOR);
FormsAuthentication.SignOut();
FormsAuthentication.SetAuthCookie(User.Identity.Name, false); //Might work in some browsers
Session["REFRESHROLES"] = User.Identity.Name;
Response.Redirect("someprotectedurl?someid=" + someid);

2) On the login page, log in again if the username is stored in the session

protected void Page_Load(object sender, EventArgs e)
{
   string returnUrl = Request.QueryString["ReturnUrl"];
   if(String.IsNullOrEmpty(returnUrl) == false)
   {

         if(Session["REFRESHROLES"] != null)
         {
            if(!string.IsNullOrEmpty(Session["REFRESHROLES"].ToString()))
            {

               FormsAuthentication.SetAuthCookie(Session["REFRESHROLES"].ToString(), false);
               Session.Remove("REFRESHROLES");
               Response.Redirect(returnUrl);  
               return;
            }
         }
0
source

Source: https://habr.com/ru/post/1727406/


All Articles