我遇到了ASP.NET Core中的身份验证管道的一些问题。我的方案是我想向已经使用OpenID Connect和Azure AD进行身份验证的用户发出挑战。有多种场景可供您使用,例如在AAD v2端点场景中请求其他范围时。在ASP.NET核心中重新验证经过身份验证的用户
这很像ASP.NET MVC中的魅力,但在ASP.NET Core MVC中,用户正被重定向到Cookie身份验证中间件中配置的Access Denied页面。 (当用户没有登录时,发出挑战按预期工作。)
经过几个小时搜索网络并尝试不同的参数为我的中间件选项,我开始怀疑,或者我失踪明显的东西,或者这种行为是通过设计,我需要以其他方式解决我的需求。任何人对此有任何想法?
编辑:我Startup.cs的相关部分是这样的:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication(
SharedOptions => SharedOptions.SignInScheme = CookieAuthenticationDefaults.AuthenticationScheme);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
// <snip...>
app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme });
var options = new OpenIdConnectOptions
{
AuthenticationScheme = OpenIdConnectDefaults.AuthenticationScheme,
ClientId = ClientId,
Authority = Authority,
CallbackPath = Configuration["Authentication:AzureAd:CallbackPath"],
ResponseType = OpenIdConnectResponseType.CodeIdToken,
PostLogoutRedirectUri = "https://localhost:44374/",
TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters
{
ValidateIssuer = false
}
};
options.Scope.Add("email");
options.Scope.Add("offline_access");
app.UseOpenIdConnectAuthentication(options);
}
和动作看起来是这样的:
public void RefreshSession()
{
HttpContext.Authentication.ChallengeAsync(OpenIdConnectDefaults.AuthenticationScheme, new AuthenticationProperties { RedirectUri = "/" });
}
您是否考虑使用AuthenticationSchemes?如果您想忽略某些操作的cookieauthentication,则可以使用另一种方案(如azureaadscheme)的Authorize特性。 –
不幸的是,这也没有帮助。我可以设置所有我想要的方案并为挑战指定一个方案,但Cookie身份验证仍然以某种方式涉及处理它。请注意,我没有使用Authorize属性。用户已经通过身份验证,并且我手动发起挑战。 – VolatileCoder
你可以发布startup.cs和操作方法吗? –