Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Setting HttpContext.Current.User

  • 24-07-2009 08:01PM
    #1
    Registered Users, Registered Users 2 Posts: 4,325 ✭✭✭


    I have an ASP.NET site and I am logging in users without using the ASP.NET login control. I can assign them to roles once they login.
    See example code below:
     string[] roles = {"Admin"};
          
            System.Security.Principal.GenericIdentity curIdentity = null;
            System.Security.Principal.GenericPrincipal curPrincipal = null;
                curIdentity = new System.Security.Principal.GenericIdentity(userName);
                curPrincipal = new System.Security.Principal.GenericPrincipal(curIdentity, roles);
                HttpContext.Current.User = curPrincipal;
    
     if (HttpContext.Current.User.IsInRole("Admin"))
               {
                System.Windows.Forms.MessageBox.Show("This user is in admin role");
               }
    
    
    The user is in the role I want them to be but the settings I have placed in my Web.config file are not being picked up.

    Web.config section below:
    
     <location path="Page1.aspx">
        <system.web>
          <authorization>
          <deny roles="Admin">        
          </deny>       
          </authorization>
        </system.web>    
      </location>
    

    The "Page1.aspx" file should not be accessible to a user in the Admin role but it is. This is because the "HttpContext.Current.User" is not being recognised outside of the page it is in. How can I change this? I've seen some code on the web that might fix it but it looks a right nightmare.


Comments

Advertisement