【发布时间】:2020-02-10 18:22:04
【问题描述】:
我正在使用 WsFed 将 ADFS SSO 实施到应用程序中。如果我尝试运行[Authorize] 方法,我会被带到登录页面。当我登录时,会创建一个带有加密信息的 cookie,并且我可以运行 [Authorize] 方法。 cookie 有选项ExpireTimeSpan = TimeSpan.FromSeconds(10);。到目前为止,这按预期工作,未经授权的用户无法访问该应用程序。
当 cookie 过期、被更改或从浏览器中删除时,混乱就开始了。发生这种情况时,如果我运行 [Authorized] 方法,我会自动再次登录,而无需重新输入我的凭据并重新创建 cookie。但是,如果我使用 return SignOut(... 方法明确退出,则需要重新输入我的凭据。
如果我删除了 cookie,为什么 ADFS 会重新对我进行身份验证,它是如何知道这样做的?如果我明确退出,它不会这样做。保持身份验证不应该取决于具有正确值的 cookie 吗?
Startup.ConfigureServices 中的身份验证设置:
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(options =>
{
options.Wtrealm = Configuration["AppSettings:wsfed:realm"];
options.MetadataAddress = Configuration["AppSettings:wsfed:metadata"];
options.UseTokenLifetime = false;
})
.AddCookie(options =>
{
options.Cookie.Name = "AuthenticationCookie";
options.LoginPath = "/signin-wsfed";
options.LogoutPath = "/NameController/Logout";
options.ExpireTimeSpan = TimeSpan.FromSeconds(10);
options.SlidingExpiration = true;
});
登录操作:
[AllowAnonymous]
[HttpGet]
public IActionResult Login()
{
var authProperties = new AuthenticationProperties
{
RedirectUri = "https://app:1234/NameController/Index",
};
return Challenge(authProperties, WsFederationDefaults.AuthenticationScheme);
}
注销操作:
[AllowAnonymous]
[HttpGet]
public IActionResult SignOutOfADFS()
{
return SignOut(
new AuthenticationProperties
{
RedirectUri = "https://app:1234/NameController/AfterLogout"
},
CookieAuthenticationDefaults.AuthenticationScheme,
WsFederationDefaults.AuthenticationScheme);
}
【问题讨论】:
标签: asp.net-mvc authentication asp.net-core cookies adfs