I've added OpenID Authentication to my ASP.NET Core 2.0 wep app:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddOpenIdConnect(option =>
{
option.ClientId = Configuration["AzureAD:ClientId"];
option.Authority = String.Format(Configuration["AzureAd:AadInstance"], Configuration["AzureAd:Tenant"]);
option.SignedOutRedirectUri = Configuration["AzureAd:PostLogoutRedirectUri"];
});
How do I turn on automatic challenge, so controller, resp action with AuthorizeAttribute will return 403 rather than redirect?
EDIT: I ended up with this:
.AddOpenIdConnect(option =>
{
...
option.Events = new OpenIdConnectEvents
{
OnRedirectToIdentityProvider = context =>
{
bool isAjaxRequest = context.HttpContext.Request.Headers["x-requested-with"] == "XMLHttpRequest";
if (isAjaxRequest)
{
context.HttpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
//context.HttpContext.Response.Headers["Location"] = ???request.RedirectUrl;
context.HandleResponse();
}
return Task.CompletedTask;
}
};
});
Although I don't want to redirect Ajax request (because why?), I would like to pass the redirect url to the client. How to get the RedirectURL?