public async Task ValidateAsync(ExtensionGrantValidationContext context) { try { var token = context.Request.Raw["token"]; var userId = await GetFacebookUserIdFromTokenAsync(context, token); if (userId == null) { return; } _logger.LogDebug("FacebookSignIn: Signing in: {0}", userId); User user = await GetOrCreateUserAsync(userId); var claims = await _userClaimsProvider.GetUserClaimsAsync(user); context.Result = new GrantValidationResult(user.UserId, "nether-facebook", claims); _appMonitor.LogEvent("LoginSucceeded", properties: new Dictionary <string, string> { { "LoginType", "fb-usertoken" } }); } catch (Exception ex) { _logger.LogError("Error in ValidateAsync: {0}", ex); _appMonitor.LogError(ex, "Error in ValidateAsync", new Dictionary <string, string> { { "EventType", "LoginFailed" }, { "EventSubType", "UnhandledException" }, { "LoginType", "fb-usertoken" } }); context.Result = new GrantValidationResult(TokenRequestErrors.InvalidRequest, ex.Message); } }
public async Task ValidateAsync(ExtensionGrantValidationContext context) { try { var token = context.Request.Raw["token"]; var facebookTokenDebug = await _facebookGraphService.TokenDebugAsync(token); if (!facebookTokenDebug.IsValid) { var message = (string)facebookTokenDebug.Error.Message; _logger.LogError("FacebookSignIn: invalid token: {0}", message); _appMonitor.LogEvent("LoginFailed", $"FacebookSignIn: invalid token: {message}", new Dictionary <string, string> { { "EventSubType", "InvalidToken" }, { "LoginType", "fb-usertoken" } }); context.Result = new GrantValidationResult(TokenRequestErrors.InvalidRequest); return; } if (facebookTokenDebug.Error != null) // still got another error { var message = (string)facebookTokenDebug.Error.Message; _logger.LogError("FacebookSignIn: error validating token: {0}", message); _appMonitor.LogEvent("LoginFailed", $"FacebookSignIn: error validating token: {message}", new Dictionary <string, string> { { "EventSubType", "TokenValidationFailed" }, { "LoginType", "fb-usertoken" } }); context.Result = new GrantValidationResult(TokenRequestErrors.InvalidRequest); return; } var userId = facebookTokenDebug.UserId; if (userId == null) { return; } _logger.LogDebug("FacebookSignIn: Signing in: {0}", userId); User user = await GetOrCreateUserAsync(userId); var claims = await _userClaimsProvider.GetUserClaimsAsync(user); context.Result = new GrantValidationResult(user.UserId, "nether-facebook", claims); _appMonitor.LogEvent("LoginSucceeded", properties: new Dictionary <string, string> { { "LoginType", "fb-usertoken" } }); } catch (Exception ex) { _logger.LogError("Error in ValidateAsync: {0}", ex); _appMonitor.LogError(ex, "Error in ValidateAsync", new Dictionary <string, string> { { "EventType", "LoginFailed" }, { "EventSubType", "UnhandledException" }, { "LoginType", "fb-usertoken" } }); context.Result = new GrantValidationResult(TokenRequestErrors.InvalidRequest, ex.Message); } }
private void EnsureInitialAdminUser(IApplicationBuilder app, IConfiguration configuration, ILogger logger) { IApplicationPerformanceMonitor appMonitor = null; try { var serviceProvider = app.ApplicationServices; appMonitor = serviceProvider.GetService <IApplicationPerformanceMonitor>(); logger.LogInformation("Identity:Store: Checking user store..."); // construct a context to test if we have a user var identityContext = serviceProvider.GetRequiredService <IdentityContextBase>(); bool gotUsers = identityContext.Users.Any(u => u.Role == RoleNames.Admin); if (gotUsers) { logger.LogInformation("Identity:Store: users exist - no action"); } else { logger.LogInformation("Identity:Store: Adding initial admin user..."); // Create an initial admin var passwordHasher = serviceProvider.GetRequiredService <IPasswordHasher>(); var password = configuration["Identity:InitialSetup:AdminPassword"]; var user = new UserEntity { Role = RoleNames.Admin, IsActive = true, Logins = new List <LoginEntity> { new LoginEntity { ProviderType = LoginProvider.UserNamePassword, ProviderId = "netheradmin", ProviderData = passwordHasher.HashPassword(password) } } }; user.Logins[0].User = user; identityContext.Users.Add(user); identityContext.SaveChanges(); logger.LogInformation("Identity:Store: Adding initial admin user... complete"); } } catch (Exception ex) { logger.LogCritical("Identity:Store: Adding initial admin user, exception: {0}", ex); appMonitor.LogError(ex, "Error adding initial admin user"); } }
private async Task SendScoreEventAndLogErrors(ScorePostRequestModel request) { try { await _analyticsIntegrationClient.SendGameEventAsync(new ScoreEvent() { //GamerTag = gamertag, ClientUtcTime = DateTime.UtcNow, GameSessionId = "unknowngamesession", Score = request.Score }); } catch (Exception ex) { _logger.LogError("Error sending analytics ScoreEvent: {0}", ex); _appMonitor.LogError(ex, properties: new Dictionary <string, string> { { "Score", request.Score.ToString() } }); } }