tags:

views:

6

answers:

1

I have an ASP.NET webforms website that uses the .NET MySql connector and MySql as a back-end. I'm using role-based authentication to protect certain areas of my site. The problem is that the user's role information is not being persisted after I move my site to the server (works fine on my local development machine). What is happening is that I can login using the logincontrol which directs me to the admin area after authenticating the user. At this point User.IsInRole("admin") is true. But when I click a link to go to a different page in the admin section User.IsInRole("admin") is false.

Web.config

 <membership defaultProvider="MySqlMembershipProvider" userIsOnlineTimeWindow="15">  
     <providers>  
         <remove name="MySQLMembershipProvider"/>  
         <add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/" requiresUniqueEmail="true" passwordFormat="Clear" maxInvalidPasswordAttempts="3" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="3" passwordStrengthRegularExpression="" autogenerateschema="true"/>  
     </providers>  
 </membership>  
 <profile>  
     <providers>  
         <remove name="MySQLProfileProvider"/>  
         <add name="MySQLProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider, MySql.Web, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/"/>  
     </providers>  
 </profile>    
 <roleManager enabled="true" defaultProvider="MySqlRoleProvider">  
     <providers>  
         <remove name="MySqlRoleProvider"/>  
         <add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.2.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/"/>  
     </providers>  
 </roleManager>  

Global.asax

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null) {
            if (Request.IsAuthenticated == true) {
                FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(Context.Request.Cookies[FormsAuthentication.FormsCookieName].Value);
                // In this case, ticket.UserData = "Admin"                
                string[] roles = new string[1] { ticket.UserData };
                FormsIdentity id = new FormsIdentity(ticket);
                Context.User = new System.Security.Principal.GenericPrincipal(id, roles);
            }
        }
    }
A: 

Are you using different data sources in development and on the server (I do)? If so, make sure your data is in sync. This is likely the case only if you specify (local) or . as the server in your database connection.

Otherwise, output to a log to see if the user roles ever contain "admin".

Brad