【发布时间】:2020-02-03 17:00:04
【问题描述】:
我正在设置没有身份的 WS-Federation,但我不知道如何更改用户通过身份验证的时间长度。我什至不知道它默认过期需要多长时间。
我尝试在我的ConfigureServices 方法中设置ExpireTimeSpan 和Cookie.Expiration cookie 选项,但这给了我HTTP ERROR 500。
如果我在登录时删除了TestAuthCookie cookie,它只会在下一个仅授权操作中重新创建,而无需再次要求登录。
我还尝试在我的控制器中使用SignIn() 方法,因为它有一个ExpiresUtc 选项,但这似乎没有任何作用。
我想要的是在登录 30 秒后使身份验证过期。如果用户在 30 秒后尝试仅授权的操作,他们将需要再次登录。
编辑:似乎当我用CookieAuthenticationDefaults.AuthenticationScheme 替换我的SignIn 方法中的方案时,凭据确实会过期。但是,当我单击登录按钮(或任何需要授权的操作)时,用户会自动登录并重新创建 cookie。但我希望用户需要重新登录。
Startup.ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(options =>
{
options.Wtrealm = Configuration["wsfed:realm"];
options.MetadataAddress = Configuration["wsfed:metadata"];
})
.AddCookie(options =>
{
options.Cookie.Name = "TestAuthCookie";
//options.ExpireTimeSpan = TimeSpan.FromSeconds(30);
//options.Cookie.Expiration = TimeSpan.FromSeconds(30);
});
services.AddControllersWithViews();
}
尝试在控制器操作中使用 SignIn 方法:
[Authorize]
public IActionResult Login()
{
var personClaims = new List<Claim>()
{
new Claim(ClaimTypes.Name, "PersonName"),
new Claim(ClaimTypes.Email, "person@email.com")
};
var personIdentity = new ClaimsIdentity(personClaims, "Person Identity");
var userPrincipal = new ClaimsPrincipal(new[] { personIdentity });
return SignIn(userPrincipal,
new Microsoft.AspNetCore.Authentication.AuthenticationProperties
{
RedirectUri = "https://app:1234/",
ExpiresUtc = DateTimeOffset.UtcNow.AddSeconds(30)
}, WsFederationDefaults.AuthenticationScheme);
}
【问题讨论】:
标签: asp.net-mvc asp.net-core cookies authorization ws-federation