如何将新创建的用户添加到Sensenet中的特定安全组?

时间:2019-02-02 15:05:31

标签: sensenet

当用户在我的Web应用程序中注册帐户时,我希望将他们添加到安全组中已标识的用户,以便他们具有运行我的Web应用程序所需的权限。这就是我尝试过的。

using SenseNet.ContentRepository.Storage;
using SenseNet.ContentRepository.Storage.Security;

namespace DerAssistantService.Actions
{
  public static class UserActions
  {
    [ODataAction]
    public static Content RegisterUser(Content content, string email, string password)
    {
        if (string.IsNullOrEmpty(email))
            throw new ArgumentNullException(nameof(email));
        if (string.IsNullOrEmpty(password))
            throw new ArgumentNullException(nameof(password));

        var username = email.Split('@').First();

        using (new SystemAccount())
        {
            var user = Content.CreateNew("User", content.ContentHandler, username);

            user["FullName"] = username;
            user["Email"] = email;
            user["LoginName"] = email;
            user["Enabled"] = true;
            user["Password"] = password;
            user.Save();

            var identifiedUsers = Node.Load<Group>("/Root/IMS/BuiltIn/Portal/IdentifiedUsers");
            identifiedUsers.AddMember(user); // Error because type Content is not of type IGroup 

            return user;
        }
     }
  }
}

1 个答案:

答案 0 :(得分:1)

组类的AddMember方法需要一个IUser或一个IGroup实例。先前创建的用户是Content类型的用户,这是Sensenet用于所有内容的包装器类型。基础业务对象位于该内容对象的内部,您可以使用ContentHandler属性将其提取:

identifiedUsers.AddMember(user.ContentHandler as IUser); 

Content对象代表上层通用API层,例如,您在其中查找字段。可以通过ContentHandler属性访问的较低层代表具有强类型类的业务层,例如UserFileWorkspace

相关问题