public async Task<User> CreateExternalAsync(ExternalLoginModel externalInfo)
        {
            var userRegistration = new UserRegistration()
            {
                Email = externalInfo.Email,
                FullName = externalInfo.FullName,
                Avatar = await GetExternalAvatarAsync(externalInfo),
                ExternalLoginInfo = new ExternalLoginInfo
                {
                    ProviderType = externalInfo.Provider,
                    ProviderKey = externalInfo.ProviderKey
                }
            };

            return await this.UsersManager.CreateUserAsync(userRegistration);
        }
        public Task<User> CreateUserAsync(UserRegistration userRegistration)
        {
            return Task<User>.Factory.StartNew(() =>
            {
                return this.BaseDal.Execute(IsolationLevel.Serializable,
                (tran) =>
                {
                    //Check if external login is unique
                    if (userRegistration.ExternalLoginInfo != null)
                    {
                        if (this.UsersDal.GetUser(tran,
                            userRegistration.ExternalLoginInfo.ProviderType,
                            userRegistration.ExternalLoginInfo.ProviderKey) != null)

                            throw new ApiException(string.Format(Exceptions.ExternalLoginAlreadyExists,
                                userRegistration.ExternalLoginInfo.ProviderType));
                    }

                    //Check login is unique if registration via password
                    UserDb dbUser = this.UsersDal.GetUser(tran, userRegistration.Email);
                    if (dbUser != null && !string.IsNullOrEmpty(dbUser.Password) && !string.IsNullOrEmpty(userRegistration.Password))
                        throw new ApiException(string.Format(Exceptions.UserAlreadyRegistered, userRegistration.Email));

                    //Create user if doesn't exist
                    bool isNewUser = (dbUser == null);
                    if (isNewUser)
                        dbUser = this.UsersDal.CreateUser(tran, userRegistration);

                    //If external, create extLogin record
                    if (userRegistration.ExternalLoginInfo != null)
                        this.UsersDal.CreateUserExtLoginInfo(tran, dbUser.Id, userRegistration.ExternalLoginInfo);

                    //If avatar specified -> save to DB
                    if (userRegistration.Avatar != null)
                    {
                        if (isNewUser)
                            this.UsersDal.CreateUserAvatar(tran, dbUser.Id, userRegistration.Avatar);
                        else
                            this.UsersDal.UpdateUserAvatar(tran, dbUser.Id, userRegistration.Avatar);
                    }

                    return dbUser;
                });
            });
        }
Пример #3
0
        public UserDb CreateUser(SqlTransaction transaction, UserRegistration userRegistration)
        {
            using (var cmd = new SqlCommand("[dbo].[spCreateUser]", transaction.Connection, transaction))
            {
                var createdDate = DateTime.Now;
                var verifyEmailCode = Guid.NewGuid();

                cmd.Parameters.AddWithValue("email", userRegistration.Email);
                cmd.Parameters.AddWithValue("password", Helper.ToSqlNullable(userRegistration.Password));
                cmd.Parameters.AddWithValue("fullName", Helper.ToSqlNullable(userRegistration.FullName));
                cmd.Parameters.AddWithValue("createdDate", createdDate);
                cmd.Parameters.AddWithValue("updatedDate", createdDate);
                cmd.Parameters.AddWithValue("verifyEmailCode", verifyEmailCode);
                cmd.CommandType = CommandType.StoredProcedure;

                return GetSingleUser(cmd);
            }
        }