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

AddUserToRole User cannot be found

$
0
0

Hi,

I am trying to add a user to a role, in the example below which is just to explain the issue at the various points, I create a user, check the user exists with GetUser, check the role exists, and then add the user to the role. The problem I am having is that when I call AddUserToRole I get an error "The user cannot be found", which I can't understand, especially as I actually pass the user name from the MembershipUser.UserName.

MembershipUser user = Membership.Provider.GetUser(txtUserName.Value, false); //Get user
                if (user != null) //Does user exist
                {
                    Membership.DeleteUser(txtUserName.Value); //Delete the user
                }
                try
                {
                    Globals.gloE.grabEmail(txtUserName.Value, Globals.ip, Globals.basedn);
                    string email = Globals.gloE.gotEmail;
                    if (email == "")
                    {
                        Response.Redirect("login.aspx", true);
                    }
                    var check = Membership.Provider.GetUser(txtUserName.Value, false); // Check to see if we did actually delete the user
                    MembershipCreateStatus createStatus;
                    Membership.CreateUser(txtUserName.Value, txtUserPass.Value, email, null, null, true, out createStatus); //Create the user again
                    if (createStatus == MembershipCreateStatus.Success)
                    {
                        //Creation successful
                        user = Membership.Provider.GetUser(txtUserName.Value, true);

                        string dn = Globals.gloS.fullDN;
                        if (dn.Contains("ou=ITS"))
                        {
                            //Get all roles
                            string[] roleArr = System.Web.Security.Roles.GetAllRoles();
                            try
                            {
                                //Check if user already has the role we want to assign
                                if (!System.Web.Security.Roles.IsUserInRole(user.UserName, "ITS"))
                                {
                                    //Check if the role exists
                                    if (roleArr.Contains("ITS"))
                                    {
                                        //Role exists, add to the user we just created
                                        System.Web.Security.Roles.AddUserToRole(user.UserName, "ITS");
                                    }
                                    else
                                    {
                                        //Create the role
                                        System.Web.Security.Roles.CreateRole("ITS");
                                        //Add the role we created to the user we just created
                                        System.Web.Security.Roles.AddUserToRole(user.UserName, "ITS");
                                    }
                                }
                            }
                            catch (Exception exRole)
                            {

                            }
                        }
                    }
                }
                catch
                {
                    Response.Redirect("login.aspx", true);
                }

Here is my role provider:

authentication mode="Forms"><forms name=".ASPXFORMSDEMO" loginUrl="Account/login.aspx" protection="All" path="/" timeout="30" /></authentication><profile defaultProvider="DashProfileProvider" enabled="true"><providers><clear /><add name="DashProfileProvider" 
             type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=dfsgdfhrhb66456" 
             connectionStringName="DashSecurity" 
             applicationName="Dash" /></providers></profile><membership defaultProvider="DashMembershipProvider"><providers><clear /><!--Add a customized SqlMembershipProvider --><add name="DashMembershipProvider" 
             type="System.Web.Security.SqlMembershipProvider" 
             connectionStringName="DashSecurity"
             enablePasswordRetrieval="false" 
             enablePasswordReset="true" 
             requiresQuestionAndAnswer="false" 
             applicationName="Dash" 
             requiresUniqueEmail="true" 
             passwordFormat="Hashed" 
             maxInvalidPasswordAttempts="5" 
             minRequiredPasswordLength="6" 
             minRequiredNonalphanumericCharacters="0" 
             passwordAttemptWindow="10" 
             passwordStrengthRegularExpression="" /></providers></membership><roleManager enabled="true" 
                 defaultProvider="DashRoleProvider" 
                 cacheRolesInCookie="true" 
                 cookieName=".myroles" 
                 cookieTimeout="30" 
                 cookieSlidingExpiration="true" 
                 cookieProtection="All"><providers><clear /><add name="DashRoleProvider" 
             type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=dfsgdfhrhb66456" 
             connectionStringName="DashSecurity" 
             applicationName="Dash" /></providers></roleManager><!--
            If you are deploying to a cloud environment that has multiple web server instances,
            you should change session state mode from "InProc" to "Custom". In addition,
            change the connection string named "DefaultConnection" to connect to an instance
            of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
      --><sessionState mode="InProc" customProvider="DashSessionProvider"><providers><clear /><add name="DashSessionProvider" 
             type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=dfsgdfhrhb66456" 
             connectionStringName="DashSecurity" /></providers></sessionState>


The public key tokens are just made up in this example.

Many thanks,

Andy


Viewing all articles
Browse latest Browse all 4737

Trending Articles



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