Last active
August 28, 2019 19:16
-
-
Save ms-willfid/813dd19091dfa8650895182cb45d5d1c to your computer and use it in GitHub Desktop.
Asp.Net Core OnAuthenticationFailed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.Configure<CookiePolicyOptions>(options => | |
{ | |
// ... | |
}); | |
services.AddAuthentication(AzureADDefaults.AuthenticationScheme) | |
.AddAzureAD(options => Configuration.Bind("AzureAd", options)); | |
// ... | |
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options => | |
{ | |
options.Authority = options.Authority; | |
// Token Validation | |
options.TokenValidationParameters.IssuerValidator = AadIssuerValidator.ValidateAadIssuer; | |
// Response type | |
options.ResponseType = "id_token code"; | |
// On Authorization Code Received | |
options.Events.OnAuthorizationCodeReceived = async context => | |
{ | |
// ... | |
}; | |
// On Authentication Failed | |
options.Events.OnAuthenticationFailed = async context => | |
{ | |
String ErrorMessage = context.Exception.Message; | |
String InnerErrorMessage = String.Empty; | |
String RedirectError = String.Format("?error_message={0}", ErrorMessage); | |
if (context.Exception.InnerException != null) | |
{ | |
InnerErrorMessage = context.Exception.InnerException.Message; | |
RedirectError = String.Format("{0}&inner_error={1}", RedirectError, InnerErrorMessage); | |
} | |
// or you can just throw it | |
// throw new Exception(RedirectError); | |
RedirectError = RedirectError.Replace("\r\n", " "); | |
context.Response.Redirect(RedirectError); | |
context.HandleResponse(); | |
}; | |
// ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment