public CreateUserStatus CreateUser(RegistrationData user,
            [Required(ErrorMessageResourceName = "ValidationErrorRequiredField",
            ErrorMessageResourceType = typeof (ValidationErrorResources))] [StringLength(50, MinimumLength = 1,
            ErrorMessageResourceName = "ValidationErrorBadPasswordLength",
            ErrorMessageResourceType = typeof (ValidationErrorResources))] string
            password)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            // Run this BEFORE creating the user to make sure roles are enabled and the default role
            // will be available
            //
            // If there are a problem with the role manager it is better to fail now than to have it
            // happening after the user is created
            if (!Roles.RoleExists(DefaultRole))
            {
                Roles.CreateRole(DefaultRole);
            }

            // NOTE: ASP.NET by default uses SQL Server Express to create the user database.
            // CreateUser will fail if you do not have SQL Server Express installed.
            MembershipCreateStatus createStatus;
            Membership.CreateUser(user.UserName, password, user.Email, user.Question, user.Answer, true, null,
                                  out createStatus);

            if (createStatus != MembershipCreateStatus.Success)
            {
                return ConvertStatus(createStatus);
            }

            var model = new MRZSEntities();
            aspnet_Users createdUser = model.aspnet_Users.SingleOrDefault(u => u.UserName == user.UserName);
            var adUser = new Models.User();
            adUser.FirstName = user.FirstName;
            adUser.LastName = user.LastName;
            adUser.aspnet_UserId = createdUser.UserId;
            if (user.GroupId > 0)
            {
                adUser.GroupId = user.GroupId;
            }
            model.Users.AddObject(adUser);
            model.SaveChanges();

            // Assign it to the default role
            // This *can* fail but only if role management is disabled
            if (!string.IsNullOrEmpty(user.UserRole))
                Roles.AddUserToRole(user.UserName, user.UserRole);

            // Set its friendly name (profile setting)
            // This *can* fail but only if Web.config is configured incorrectly
            ProfileBase profile = ProfileBase.Create(user.UserName, true);
            profile.SetPropertyValue("FriendlyName", user.FriendlyName);
            profile.Save();

            return CreateUserStatus.Success;
        }
Beispiel #2
0
 /// <summary>
 /// Асинхронно вызывает метод "EditUser" службы DomainService.
 /// </summary>
 /// <param name="user">Значение параметра "user" для данного действия.</param>
 /// <param name="password">Значение параметра "password" для данного действия.</param>
 /// <returns>Экземпляр операции, который может быть использован для управления асинхронным запросом.</returns>
 public InvokeOperation EditUser(RegistrationData user, [StringLength(50, ErrorMessageResourceName="ValidationErrorBadPasswordLength", ErrorMessageResourceType=typeof(ValidationErrorResources), MinimumLength=1)] string password)
 {
     Dictionary<string, object> parameters = new Dictionary<string, object>();
     parameters.Add("user", user);
     parameters.Add("password", password);
     this.ValidateMethod("EditUser", parameters);
     return this.InvokeOperation("EditUser", typeof(void), parameters, true, null, null);
 }
Beispiel #3
0
 /// <summary>
 /// Асинхронно вызывает метод "CreateUser" службы DomainService.
 /// </summary>
 /// <param name="user">Значение параметра "user" для данного действия.</param>
 /// <param name="password">Значение параметра "password" для данного действия.</param>
 /// <returns>Экземпляр операции, который может быть использован для управления асинхронным запросом.</returns>
 public InvokeOperation<CreateUserStatus> CreateUser(RegistrationData user, [Required(ErrorMessageResourceName="ValidationErrorRequiredField", ErrorMessageResourceType=typeof(ValidationErrorResources))] [StringLength(50, ErrorMessageResourceName="ValidationErrorBadPasswordLength", ErrorMessageResourceType=typeof(ValidationErrorResources), MinimumLength=1)] string password)
 {
     Dictionary<string, object> parameters = new Dictionary<string, object>();
     parameters.Add("user", user);
     parameters.Add("password", password);
     this.ValidateMethod("CreateUser", parameters);
     return ((InvokeOperation<CreateUserStatus>)(this.InvokeOperation("CreateUser", typeof(CreateUserStatus), parameters, true, null, null)));
 }
        public void EditUser(RegistrationData user,
            [StringLength(50, MinimumLength = 1,
            ErrorMessageResourceName = "ValidationErrorBadPasswordLength",
            ErrorMessageResourceType = typeof (ValidationErrorResources))] string password)
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var model = new MRZSEntities();
            Models.User createdUser = model.Users.SingleOrDefault(u => u.UserId == user.UserId);
            createdUser.FirstName = user.FirstName;
            createdUser.LastName = user.LastName;
            if (user.GroupId > 0)
            {
                createdUser.GroupId = user.GroupId;
            }
            model.SaveChanges();
            if (!string.IsNullOrEmpty(user.UserRole))
            {
                if (Roles.IsUserInRole(user.UserName, user.UserRole))
                    return;
                string[] userRoles = Roles.GetRolesForUser(user.UserName);
                if (userRoles.Length > 0)
                    Roles.RemoveUserFromRoles(user.UserName, userRoles);
                Roles.AddUserToRole(user.UserName, user.UserRole);
            }
        }