5

I followed the below article to create users programmatically

Sitecore Code Samples Programmatically Add/Edit Users

 http://sitecorecodesamples.blogspot.in/2010/11/programmatically-addedit-users.html

Working great, but the user domain always saved as blank, please suggest any configurations need full.

enter image description here

  • 1
    Did you try saving username as {domainname}/{username} ? – Balaji Kuppuswamy May 24 '17 at 2:25
  • Yes, This is my code string domain = "mydomain"; string userName = string.Format(@"{0}\{1}", domain, userName); Membership.CreateUser(userName, model.password, model.email, model.secretQuestionCode, model.secretAnswer, true, out membershipStatus); – Asdfgh May 24 '17 at 12:24
10

As @Balaji Kuppuswamy stated in comments, you simply need to include the domain name into the full user name when creating a user:

System.Web.Security.Membership.CreateUser(@"sitecore\myuser", "b", "myuser@sample.com");

Please note that the domain column on UI is fulfilled only if an existing domain is specified:

[1]: https://i.stack.imgur.com/sYa

4

As the other suggested answers suggest is correct. When you are creating a new user programmatically, you can get the Full name by using the following code:

string domainUser = Sitecore.Context.Domain.GetFullName("some@example.com");

This will give you the full user which would include the current context domain. This is what you should pass to your Create User method:

System.Web.Security.Membership.CreateUser(domainUser, "password", "some@example.com");

You can define which domain applies to the current context site, by setting the attribute in the SiteDefinition.

1

I can see Domain property of the User class doesn't have a setter, so you cannot do it programatically.

// Sitecore.Security.Accounts.User
/// <summary>
/// Gets the domain to which the user belongs.
/// </summary>
/// <value>The domain.</value>
public override Domain Domain
{
    get
    {
        if (this._domain == null)
        {
            this._domain = Domain.GetAccountDomain(this.Name);
        }
        return this._domain;
    }
}

"Domains" concept is the additional abstract level (as well as "RolesInRoles") provided by Sitecore over Asp.NET security model. In reality it's just a prefix/part of the user name, which is stored in the Core database in the "aspnet_Users". So the "admin" user from the "sitecore" domain is presented as "sitecore\admin".

So to change user domain you need to rename the user in the "aspnet_Users" table via SQL Server Managemnet Studio.

Please note that renaming the domain can cause the issues with configured access right for a user. Because access right for the particular user for the specific item are stored in this item, and mapped to user name. So when you changed user name, it will not be updated on the item level, and it can break security rights.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.