Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

The role '' is not found - error message

$
0
0
 Everything in the function below works as I would expect until  line 14 where it says they role "" was not found, even though debugger clearly shows that therole paramater is set to "admin" on the function call.

I was not having this problem until I made the switch from a mdb role data base to running the aspnet_regsql util to insert all the tables and sprocs into the normal database, but I have double checked and everything seems fine below.

Thanks for the help 

++++++++++++++++++++ 

    <connectionStrings>
        <add name="Strutster" connectionString="Data Source=(local)\SqlExpress;Initial Catalog=Strutster;Integrated Security=True" providerName="System.Data.SqlClient"/>
    <add name="MyMembershipConnStr" connectionString="Data Source=(local)\SqlExpress;Initial Catalog=Strutster;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>

<membership defaultProvider="MyMembershipProvider">
      <providers>
                <add name="MyMembershipProvider"
                         connectionString="MyMembershipConnStr"
                         applicationName="strutster"
                         enablePasswordRetrieval="true"
                         enablePasswordReset="false"
                         requiresQuestionAndAnswer="false"
                         reqiresUniqueEmail="true"
                         passwordFormat="Hashed"
                         type="System.Web.Security.SqlMembershipProvider" />
      </providers>
    </membership>

 

 

protected void LoginControl_Authenticate(object sender, AuthenticateEventArgs e)
  {
    string[] roles = Models.RoleManager.GetRoles(LoginControl.UserName, LoginControl.Password);

    if (roles != null && roles.Length > 0)
    {
      e.Authenticated = true;

      foreach (string role in roles)
      {
        if (!Roles.IsUserInRole(role))
          try
          {
            Roles.AddUserToRole(LoginControl.UserName, role);
          }
          catch (System.Configuration.Provider.ProviderException ex)
          {
            //do nothing in case the user already has the role added
          }
      }

      System.Web.Security.FormsAuthentication.RedirectFromLoginPage(LoginControl.UserName, true);
    }
  }

 
+++++

update

+++++

It seems that the reason is that it is re-creating the mdf database file when it is called on, and then in the newly created file the roles (admin, etc) do not exist.

After adding a provider element to the roleManager in the web.config everything seems to be working now just fine (in case anyone has the same issues)

 

        <roleManager enabled="true"
                                    defaultProvider="MyRoleProvider"
                                    cacheRolesInCookie="true"
                                    cookieName=".myroles"
                                    cookieTimeout="30"
                                    cookieSlidingExpiration="true"
                                    cookieProtection="All">

            <providers>
                <add name="MyRoleProvider"
                         connectionStringName="MyMembershipConnStr"
                         applicationName="**your_app_name"""
                         type="System.Web.Security.SqlRoleProvider" />
            </providers>
            
        </roleManager>

Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>