AutomaticChallenge 不适用于 ASP.NET MVC Core with Azure AD 示例中的 CookieAuthentication
AutomaticChallenge not working for CookieAuthentication in ASP.NET MVC Core with Azure AD sample
我稍微修改了 Integrating Azure AD into an ASP.NET Core web app sample 中的代码。我可以 运行 应用程序成功,如果我单击 "Log in" link 然后我正确地重定向到 Azure AD 登录页面。
但是,如果我尝试访问受 Authorize
属性保护的路由,我还希望应用程序自动将我重定向到登录页面。我已将 [Authorize]
属性添加到 HomeController
的 Contact
操作,但如果我尝试在未登录的情况下访问它,我不会被重定向到登录页面。如果我在登录时访问联系人页面,那么它会正确显示。
我已经更新了 Startup.cs
文件如下:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
LoginPath = "/Account/Login"
});
但即使进行了这些更改,我也没有被重定向到登录页面。还有什么我想念的吗?
我发现了问题。该示例引用了 Microsoft.AspNetCore.Authentication.Cookies 的 1.0.0 版。我已将软件包升级到版本 1.1.0,并且 hit the issue described here。具体来说,在此包的 1.1.0 版中,AutomaticChallenge 行为在使用多个身份验证提供程序时发生了变化(示例就是这样做的)。
我能够通过如下更新示例的 Startup.cs 文件的 Configure 方法来解决此更改:
将调用 app.UseCookieAuthentication
的行更改为
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AutomaticChallenge = true
});
并在对 app.UseOpenIdConnectAuthentication
的调用中添加此行:
AutomaticChallenge = false
我稍微修改了 Integrating Azure AD into an ASP.NET Core web app sample 中的代码。我可以 运行 应用程序成功,如果我单击 "Log in" link 然后我正确地重定向到 Azure AD 登录页面。
但是,如果我尝试访问受 Authorize
属性保护的路由,我还希望应用程序自动将我重定向到登录页面。我已将 [Authorize]
属性添加到 HomeController
的 Contact
操作,但如果我尝试在未登录的情况下访问它,我不会被重定向到登录页面。如果我在登录时访问联系人页面,那么它会正确显示。
我已经更新了 Startup.cs
文件如下:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
LoginPath = "/Account/Login"
});
但即使进行了这些更改,我也没有被重定向到登录页面。还有什么我想念的吗?
我发现了问题。该示例引用了 Microsoft.AspNetCore.Authentication.Cookies 的 1.0.0 版。我已将软件包升级到版本 1.1.0,并且 hit the issue described here。具体来说,在此包的 1.1.0 版中,AutomaticChallenge 行为在使用多个身份验证提供程序时发生了变化(示例就是这样做的)。
我能够通过如下更新示例的 Startup.cs 文件的 Configure 方法来解决此更改:
将调用 app.UseCookieAuthentication
的行更改为
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AutomaticChallenge = true
});
并在对 app.UseOpenIdConnectAuthentication
的调用中添加此行:
AutomaticChallenge = false