Newbie Question Nr.2

 

Hello everyone, I hope your all having a nice day!

Ive been given a copy of real episerver 4.60 website for training purposes.
Everything is up and running and seems to be working fine however I have one problem, I dont have any admin login.

I have been adviced to create a user from code like this: http://sdk.episerver.com/library/cms5/html/M_EPiServer_Security_WindowsMembershipProvider_CreateUser.htm
The problem is this SDK is for episerver 5 and the site is running 4.60. WindowsAuthenticationProvider dont seem to have this method in 4.60.

I searched around and found this http://world.episerver.com/en/FAQ/Items/How-do-I-create-and-manage-Users/ .
This compiles and runs but I cant login using the user I just created and I am not sure how to set the user I created to be admin.

Any Ideas?

Kind Regards
Pontus

#28867 Mar 25, 2009 13:37
  • Member since: 2008
     

    Here is a script I use:

    <script runat="server">
     private void Page_Init(object sender, System.EventArgs e)
     {
      string username = "MyAdminUser";
      string password = "MyAdminPassword";
      string[] groupNames = new string[]{ "Administrators", "WebAdmins" };
      
      EPiServer.DataAbstraction.UserSid admin = EPiServer.DataAbstraction.UserSid.Load(username);
      if (admin == null)
      {
       admin = new EPiServer.DataAbstraction.UserSid(EPiServer.Security.SecurityIdentityType.ExtranetUser);
       admin.Name = username;
       admin.Password = password;
       admin.Active = true;

       admin.Save();
            
       admin = EPiServer.DataAbstraction.UserSid.Load(username);
            
       foreach (string groupName in groupNames)
       {
        EPiServer.DataAbstraction.GroupSid group = EPiServer.DataAbstraction.GroupSid.Load(groupName);
        if (group != null)
        {
         admin.MemberOfGroups.Add(group);
        }
        else
        {
         throw new System.Exception("Failed to load group " + groupName + "!");
        }
       }
         
       admin.SaveGroupMembership();
      }
      else
      {
       throw new System.Exception("User " + username + " already exists!");
      }
        }
    </script>

    #28868 Mar 25, 2009 14:35
  • Member since: 1996
     
    I think there is a ClearCache() methodyou have to call. Dont remeber where it is
    #28870 Mar 25, 2009 14:59
  • Member since: 2006
     

    Another approach: 

    Change the authentication mode to use windows authentication instead of forms auth. (This is done in web.config)

    Then logon to the webserver(physically) with a user that is a member of the local administrators group open the 4.60 website and go into admin mode. Now you can create youself an admin account.

    Switch back to forms authentication.

    Regards,

    Morten

    #28871 Edited, Mar 25, 2009 15:12
  • Member since: 2004
     

    An addition to the post right above this - there should be no need to change the authentication mode to Windows. All administrator accounts on the computer the web server is running on have administrator rights in EPiServer 4.x as well, even if Forms authentication is used.

    Alternatively, edit your web.config file and change in <location path="admin"> from <allow roles="WebAdmins, Administrators" /> to <allow roles="*" /> - then enter admin mode and create a user which has admin rights, then change back in web.config again.

    #28872 Mar 25, 2009 15:31
  • This message was deleted by pontus2 at Mar 25, 2009 15:53.
  •  

    Ok I manged to mess up that last post, deleteing then trying to edit things back was not a good idea. Anyways:

    Tryed the code Magnus posted and it worked great!

    I dident try the solutions provided after that since I allready got it to work but they sound like good problem solvers also.

    All that remains is for you guys to start beting on how many "Newbie question" threads I posted before I get "ECD" ;)

    Have a nice day everybody and thank you everyone who contributed!

    #28874 Mar 25, 2009 15:56
  • Member since: 2008
     

    As concluded in an other post recently, developers running a Swedish (or other) version of windows where the local administrators group is not called "Administrators" ("Administratörer" in Swedish version) have to at least add the corresponding role to the <allow roles=... of their <location path=... for admin and edit to be able to log in with a local windows administrators account.

    #28896 Edited, Mar 26, 2009 9:45