示例#1
0
        /// <summary>
        /// Stores the given login credentials to the backing data store
        /// </summary>
        /// <param name="loginCredentials">The login credentials to save</param>
        /// <returns>Returns a task that will finish once the save is complete</returns>
        public async Task SaveLoginCredentialsAsync(UserProfileDetailsApiModel loginCredentials)
        {
            // Clear all entries
            mDbContext.LoginCredentials.RemoveRange(mDbContext.LoginCredentials);

            // Add new one
            await mDbContext.LoginCredentials.AddAsync(loginCredentials);

            // Save changes
            await mDbContext.SaveChangesAsync();
        }
示例#2
0
        /// <summary>
        /// Handles what happens when we have successfully logged in
        /// </summary>
        /// <param name="loginResult">The results from the successful login</param>
        public async Task HandleSuccessfulLoginAsync(UserProfileDetailsApiModel loginResult)
        {
            // Store this in the client data store
            await ClientDataStore.SaveLoginCredentialsAsync(loginResult.ToLoginCredentialsDataModel());

            //Show Login Details
            LoginDetailsVisible = true;

            // Load new settings
            await ViewModelSettings.LoadAsync();



            // Go to home page
            ViewModelApplication.GoToPage(ApplicationPage.Home);
        }
示例#3
0
        public async Task <ActionResult> UpdateProfileAsync([FromBody] UserProfileDetailsApiModel userProfile)
        {
            // Get user claims
            var user = await _userManager.GetUserAsync(HttpContext.User);

            // If we have no user return Unauthorized
            if (user == null)
            {
                // Return error
                return(Unauthorized());
            }

            user.FirstName = userProfile.FirstName;
            user.LastName  = userProfile.LastName;

            var result = await _userManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                return(Ok());
            }

            return(StatusCode(500));
        }
        public async Task <UserProfileDetailsApiModel> LogInAsync([FromBody] LoginCredentialsApiModel loginCredentials)
        {
            // TODO: Localize all strings
            // The message when we fail to login
            var invalidErrorMessage = "Invalid username or password";

            // The error response for a failed login
            var errorResponse = new UserProfileDetailsApiModel
            {
                // Set error message
                ErrorMessage = invalidErrorMessage
            };

            // Make sure we have a user name
            if (loginCredentials?.UsernameOrEmail == null || string.IsNullOrWhiteSpace(loginCredentials.UsernameOrEmail))
            {
                // Return error message to user
                return(errorResponse);
            }

            // Validate if the user credentials are correct...

            // Is it an email?
            var isEmail = loginCredentials.UsernameOrEmail.Contains("@");

            // Get the user details
            var user = isEmail ?
                       // Find by email
                       await mUserManager.FindByEmailAsync(loginCredentials.UsernameOrEmail) :
                       // Find by username
                       await mUserManager.FindByNameAsync(loginCredentials.UsernameOrEmail);

            // If we failed to find a user...
            if (user == null)
            {
                // Return error message to user
                return(errorResponse);
            }

            // If we got here we have a user...
            // Let's validate the password

            // Get if password is valid
            var isValidPassword = await mUserManager.CheckPasswordAsync(user, loginCredentials.Password);

            // If the password was wrong
            if (!isValidPassword)
            {
                // Return error message to user
                return(errorResponse);
            }

            // If we get here, we are valid and the user passed the correct login details

            // Return token to user
            return(new UserProfileDetailsApiModel
            {
                // Pass back the user details and the token
                FirstName = user.FirstName,
                LastName = user.LastName,
                Email = user.Email,
                Username = user.UserName,
                Token = user.GenerateJwtToken()
            });
        }