Esempio n. 1
0
        /// <summary>
        /// Get personal information from the database.
        /// </summary>
        /// <param name="id">User id of the user.</param>
        public StggResult <PersonalInfoManageVm> GetPersonalInfo(int id)
        {
            // Get data from the database and build the result.
            var userProfile          = Repositories.UserProfile.GetOne(x => x.UserId == id);
            var personalInfoEditorVm = new PersonalInfoManageVm(userProfile);
            var stggResult           = new StggResult <PersonalInfoManageVm>();

            // quick check on returned value.
            if (userProfile == null)
            {
                stggResult.SetStatus(StggResultStatus.Failed);
            }
            else
            {
                // profile was found - return this profile.
                stggResult.SetStatus(StggResultStatus.Succeeded);
                stggResult.SetValue(personalInfoEditorVm);
            }

            // return value
            return(stggResult);
        }
Esempio n. 2
0
        /// <summary>
        ///     Authenticate a user.
        /// </summary>
        /// <param name="appUserLoginVm">Login view model.</param>
        public async Task <StggResult <SignInStatus, AppUserVm> > LoginAsync(AppUserLoginVm appUserLoginVm)
        {
            var stggResult = new StggResult <SignInStatus, AppUserVm>();
            var appUserVm  = new AppUserVm();
            var user       = await AppUserManager.FindByNameAsync(appUserLoginVm.UserName);

            if (user != null)
            {
                var isLockedOut = await AppUserManager.IsLockedOutAsync(user.Id);

                if (AppUserManager.SupportsUserLockout && isLockedOut)
                {
                    // User is locked.
                    appUserVm.SignInStatus = SignInStatus.LockedOut;
                }
                else
                {
                    // Authenticate user.
                    if (AppUserManager.CheckPassword(user, appUserLoginVm.Password))
                    {
                        // Now we need to check and reset the Access Failed Counter for this user.
                        var getAccessFailedExceeded = AppUserManager.GetAccessFailedCount(user.Id) > 0;

                        if (AppUserManager.SupportsUserLockout && getAccessFailedExceeded)
                        {
                            // Reset the failed count.
                            var resetAccessFailResult = await AppUserManager.ResetAccessFailedCountAsync(user.Id);

                            if (resetAccessFailResult.Succeeded == false)
                            {
                                appUserVm.SignInStatus = SignInStatus.Failed;
                                stggResult.AddError("Failed to login.");
                            }
                        }

                        var emailIsConfirmed = AppUserManager.IsEmailConfirmed(user.Id);

                        if (!emailIsConfirmed)
                        {
                            appUserVm.SignInStatus = SignInStatus.RequiresVerification;
                            stggResult.AddError("Email requires verification.");
                        }

                        var userRoleIds = user.Roles.Select(e => e.RoleId);
                        var roleNames   = FindRolesByIds(userRoleIds);

                        appUserVm = new AppUserVm(user, roleNames, true);
                    }
                    else
                    {
                        var lockedoutEnabledForUser = await AppUserManager.GetLockoutEnabledAsync(user.Id);

                        // Password was not valid.
                        if (AppUserManager.SupportsUserLockout && lockedoutEnabledForUser)
                        {
                            // Increment access failed count.
                            var accessFailedResult = await AppUserManager.AccessFailedAsync(user.Id);

                            if (accessFailedResult.Succeeded == false)
                            {
                                appUserVm.SignInStatus = SignInStatus.LockedOut;
                                stggResult.AddError("User is locked out.");
                            }
                            else
                            {
                                appUserVm.SignInStatus = SignInStatus.Failed;
                            }
                        }
                        else
                        {
                            appUserVm.SignInStatus = SignInStatus.Failed;
                        }
                    }
                }
            }
            else
            {
                // We could not find this user from the database.
                appUserVm.SignInStatus = SignInStatus.Failed;
            }

            stggResult.SetValue(appUserVm);
            stggResult.SetStatus(appUserVm.SignInStatus);

            return(stggResult);
        }