コード例 #1
0
        public async Task<IdentityResult> CreateAccountAsync(string userName, string password = null)
        {
            var account = _securityRepository.Accounts.FirstOrDefault(x => x.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase));

            var user = new ApplicationUser
            {
                UserName = userName,
                Email = userName
            };

            if (account != null)
            {
                user.Id = account.AccountId;
            }

            IdentityResult result;
            if (string.IsNullOrWhiteSpace(password))
            {
                result = await UserManager.CreateAsync(user);
            }
            else
            {
                result = await UserManager.CreateAsync(user, password);
            }

            return result;
        }
コード例 #2
0
        public async Task<string> CreateUserAndAccountAsync(string userName, string password, object propertyValues = null, bool requireConfirmationToken = false)
        {

            var account = _securityRepository.Accounts.FirstOrDefault(x => x.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase));

            if (account != null)
            {
                throw new InvalidOperationException(string.Format("username {0} already taken", userName));
            }

            var user = new ApplicationUser
            {
                UserName = userName,
                Email = userName,
            };


            var result = await UserManager.CreateAsync(user, password);
            if (result.Succeeded)
            {
                IDictionary<string, object> values = propertyValues as RouteValueDictionary;

                if (values == null && propertyValues != null)
                {
                    var propertyValuesAsDictionary = propertyValues as IDictionary<string, object>;
                    values = propertyValuesAsDictionary != null ? new RouteValueDictionary(propertyValuesAsDictionary)
                                                                : new RouteValueDictionary(propertyValues);
                }

                account = new Account
                {
                    UserName = userName,
                    AccountId = user.Id
                };

                if (values != null)
                {
                    foreach (var prop in account.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(prop => values.ContainsKey(prop.Name)))
                    {
                        prop.SetValue(account, values[prop.Name]);
                    }
                }

                _securityRepository.Add(account);
                _securityRepository.UnitOfWork.Commit();

                if (requireConfirmationToken)
                {
                    return await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                }
            }

            return null;
        }