Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Asp.Net Core OnAuthenticationFailed
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
You can’t perform that action at this time.