Skip to content

Instantly share code, notes, and snippets.

@ms-willfid
Last active August 28, 2019 19:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms-willfid/813dd19091dfa8650895182cb45d5d1c to your computer and use it in GitHub Desktop.
Save ms-willfid/813dd19091dfa8650895182cb45d5d1c to your computer and use it in GitHub Desktop.
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