2016-04-15 38 views
2

我想從sitecore 8.1中的代碼創建自定義域的用戶。我成功地可以使用下面的代碼來創建用戶:如何將自定義配置文件分配給sitecore中的自定義域用戶

[HttpPost] 
    public string RegisterUser(SurveyMonkeyUser userInfo) 
    { 
     string userId = "0"; 
     string Domain = "website"; //to be taken from config 
     string ProfileItemId = "{7D473841-D585-43F6-9DF4-400313D945EC}"; //to be taken from config 

     if (!AccountExists(Domain + userInfo.Username)) 
     { 
      string domainUser = string.Concat(Domain ,@"\", userInfo.Username.Replace(" ","")); 
      var user = Sitecore.Security.Accounts.User.Create(domainUser , "pass123"); 

      user.Profile.ProfileItemId = ProfileItemId; 
      user.Profile.SetCustomProperty("FirstName", userInfo.FirstName); 
      user.Profile.SetCustomProperty("LastName", userInfo.LastName); 
      user.Profile.SetCustomProperty("Email", userInfo.Email); 
      user.Profile.SetCustomProperty("DateOfBirth", userInfo.DateOfBirth); 
      user.Profile.SetCustomProperty("Gender", userInfo.Gender); 
      user.Profile.SetCustomProperty("Location", userInfo.Location); 
      user.Profile.SetCustomProperty("Occupation", userInfo.Occupation); 
      user.Profile.SetCustomProperty("SectorEmployment", userInfo.SectorEmployment); 
      user.Profile.SetCustomProperty("Salary", userInfo.Salary); 
      user.Profile.SetCustomProperty("MaritalStatus", userInfo.MaritalStatus); 
      user.Profile.SetCustomProperty("MobileNumber", userInfo.MobileNumber); 

      user.Profile.Save(); 
      user.Profile.Reload(); 
      var username = user.Name; 
      userId = "Account Created Successfully " + user.Name;     
     } 
     else 
     { 
      userId = "user already exist"; 
     } 


     return userId; 
    } 

但它的自定義屬性是不可見在Sitecore的後端,但是調試過程中,我可以看到prevuiously創建使用Sitecore.Security.Accounts.User.FromName(@"website\SyedAhsanJaffri",true).Profile.SerializedData我得到的所有屬性

Count = 13 
    [0]: {[digestcredentialhash, a287effab21c2ec84696e4eef4ca2171]} 
    [1]: {[digestcredentialhashwithoutdomain, 767c0012faf8e872581d3ac5919db895]} 
    [2]: {[FirstName, Syed Ahsan]} 
    [3]: {[LastName, Jaffri]} 
    [4]: {[Email, [email protected]]} 
    [5]: {[DateOfBirth, 14/08/1985]} 
    [6]: {[Gender, Male]} 
    [7]: {[Location, Karachi]} 
    [8]: {[Occupation, Software Engineer]} 
    [9]: {[SectorEmployment, IT]} 
    [10]: {[Salary, 351212]} 
    [11]: {[MaritalStatus, Single]} 
    [12]: {[MobileNumber, 923312190031]} 
所有用戶的個人資料

在後端我必須手動更改自定義配置文件:)才能查看我的屬性。

+0

你這個創建自定義配置文件用戶? –

回答

5

你可以打開APP_CONFIG \ Securiry \ Domains.config和配置默認的配置文件項ID存在,例如:

<domain name="CommerceUsers" defaultProfileItemId="{0FFEC1BD-4D35-49CF-8B7D-7F01930834ED}" ensureAnonymousUser="false" /> 

附:但是你的代碼看起來是正確的。我想知道它不起作用。確保您傳輸正確的ID。仔細檢查您是否從Core數據庫(不是配置文件模板的ID)傳輸配置文件項目的ID。

+0

謝謝@Anton我使用模板ID而不是配置文件項目ID。 –

2

我通常使用user.Profile.SetPropertyValue("ProfileItemId", ProfileItemId);而不是user.Profile.ProfileItemId = ProfileItemId;。除此之外,我可以在您的代碼和我的(工作)版本之間看到的唯一區別是在設置配置文件之後並在設置自定義屬性之前,我執行保存操作(user.Profile.Save();)。

此外,像安東已經提到:確保ProfileItemId是輪廓項目的ID(位於/ Sitecore的/系統/設置/安全/配置文件/ ...)

相關問題