public AuthentificationResult Authenticate(string userName, string password) { var result = new AuthentificationResult(); User user; try { user = this.userService.GetUserByLoginPassword(userName, password); result.User = user; } catch (PostException) { result.ErrorMessage = "Invalid user name or password."; return(result); } var tkey = Encoding.ASCII.GetBytes(options.Value.JwtSecret); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString()), new Claim(ClaimTypes.Role, user.RoleId.ToString()) }), Expires = DateTime.UtcNow.AddDays(7), SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(tkey), SecurityAlgorithms.HmacSha256Signature) }; var token = this.jwtSecurityTokenHandler.CreateToken(tokenDescriptor); result.Token = jwtSecurityTokenHandler.WriteToken(token); return(result); }
public async Task <AuthentificationResult> CheckAuthentificationAsync(UserInfo userInfo = null) { if (userInfo == null) { this.logger.Debug("CheckAuthentificationAsync: trying to restore previos session."); var userSession = this.sessionService.GetSession(); if (userSession != null) { this.logger.Debug("CheckAuthentificationAsync: GetSession is not null."); var cookieCollection = await this.sessionService.GetSavedCookiesAsync(); if (cookieCollection != null) { this.logger.Debug("CheckAuthentificationAsync: cookie collection is not null. Initializing web services."); this.googleMusicWebService.Initialize(cookieCollection.Cast <Cookie>()); userSession.IsAuthenticated = true; return(AuthentificationResult.SucceedResult()); } } } if (userInfo == null) { this.logger.Debug("CheckAuthentificationAsync: Trying to get user info with pasword."); userInfo = this.googleAccountService.GetUserInfo(retrievePassword: true); } if (userInfo == null) { this.logger.Debug("CheckAuthentificationAsync: Cannot get user info."); return(AuthentificationResult.FailedResult(null)); } GoogleAuthResponse authResponse = await this.googleAccountWebService.AuthenticateAsync( new Uri(this.googleMusicWebService.GetServiceUrl()), userInfo.Email, userInfo.Password); if (authResponse.Success) { if (authResponse.CookieCollection != null && authResponse.CookieCollection.Count > 0) { this.googleMusicWebService.Initialize(authResponse.CookieCollection.Cast <Cookie>()); await this.googleMusicWebService.SaveCurrentSessionAsync(); this.sessionService.GetSession().IsAuthenticated = true; return(AuthentificationResult.SucceedResult()); } } else if (authResponse.Error.HasValue) { string errorMessage = this.GetErrorMessage(authResponse.Error.Value); this.logger.Warning("CheckAuthentificationAsync: ErrorMessage: {0}, error code: {1}", errorMessage, authResponse.Error.Value); return(AuthentificationResult.FailedResult(errorMessage)); } this.logger.Error("CheckAuthentificationAsync: showing 'Login_Unknown'."); return(AuthentificationResult.FailedResult(this.resources.GetString("Authorization_Error_Unknown"))); }
public static void BuildResponse(HttpListenerContext context, AuthentificationResult authentificationResult) { if (authentificationResult == null) { throw new ArgumentNullException(nameof(authentificationResult)); } BuildResponse(context, authentificationResult.Status, authentificationResult.AccountView, authentificationResult.AdditionalHeaders); }