public async Task <ActionResult <AuthenticationResult> > Post([FromBody] LoginInfo loginInfo) { WechatToken wechatToken; try { wechatToken = await _wechatApi.GetWechatToken(loginInfo.WechatCode); } catch (WechatApiException) { return(AuthenticationResult.Failure("无效的 Wechat code")); } catch (Exception ex) { _logger.LogError(ex, "通过微信 API 获取 access_code 时发生异常:{0}:{1}", ex.GetType(), ex.Message); throw; } UserEntity userEntity; try { userEntity = await _dataFacade.AddOrFindUserByWechatId(wechatToken.OpenId); } catch (Exception ex) { _logger.LogError(ex, "根据微信 ID 向数据源插入或查找用户数据时抛出异常:{0}:{1}", ex.GetType(), ex.Message); throw; } var token = new AuthenticationToken(userEntity.Id, userEntity.IsAdmin, wechatToken); return(AuthenticationResult.Success(userEntity.Id, userEntity.IsAdmin, _jwt.Encode(token))); }
public AuthenticationResult Authenticate(string username, string password) { EventStream stream; Guid? userId; using (_databaseContextFactory.Create(_configuration.ProviderName, _configuration.ConnectionString)) { userId = _keyStore.Get(User.Key(username)); if (!userId.HasValue) { _log.Trace(string.Format("[username not found] : username = '******'", username)); return(AuthenticationResult.Failure()); } stream = _eventStore.Get(userId.Value); } var user = new User(userId.Value); stream.Apply(user); if (user.PasswordMatches(_hashingService.Sha256(password))) { return(AuthenticationResult.Success()); } _log.Trace(string.Format("[invalid password] : username = '******'", username)); return(AuthenticationResult.Failure()); }
public static async Task <AuthenticationResult> AggregateResultsAsync <TProvider>(this IEnumerable <TProvider> providers, Func <TProvider, Task <AuthenticationResult> > resultResolver) { // If no providers are supplied, return an empty unsuccessful result. if (providers == null) { return(AuthenticationResult.Skip); } var failureResults = new HashSet <AuthenticationResult>(); // TODO: Serial vs parallel? foreach (var provider in providers) { var result = await resultResolver(provider).ConfigureAwait(false); if (result == null || result.Skipped) { continue; } if (result.Successful) { return(result); } failureResults.Add(result); } switch (failureResults.Count) { case 0: return(AuthenticationResult.Skip); case 1: return(failureResults.First()); default: // Aggregate all the error messages into a new AuthenticationResult var errors = failureResults.SelectMany(r => r.Errors).Distinct().ToList(); return(errors.Count == 0 ? AuthenticationResult.Skip : AuthenticationResult.Failure(errors)); } }