【问题标题】:WS-Federation authentication expiration timeWS-Federation 认证过期时间
【发布时间】:2020-02-03 17:00:04
【问题描述】:

我正在设置没有身份的 WS-Federation,但我不知道如何更改用户通过身份验证的时间长度。我什至不知道它默认过期需要多长时间。

我尝试在我的ConfigureServices 方法中设置ExpireTimeSpanCookie.Expiration cookie 选项,但这给了我HTTP ERROR 500。 如果我在登录时删除了TestAuthCookie cookie,它只会在下一个仅授权操作中重新创建,而无需再次要求登录。 我还尝试在我的控制器中使用SignIn() 方法,因为它有一个ExpiresUtc 选项,但这似乎没有任何作用。

我想要的是在登录 30 秒后使身份验证过期。如果用户在 30 秒后尝试仅授权的操作,他们将需要再次登录。

编辑:似乎当我用CookieAuthenticationDefaults.AuthenticationScheme 替换我的SignIn 方法中的方案时,凭据确实会过期。但是,当我单击登录按钮(或任何需要授权的操作)时,用户会自动登录并重新创建 cookie。但我希望用户需要重新登录。

我使用的指南:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/ws-federation?view=aspnetcore-3.0

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


    【解决方案1】:

    试试这个方法生成cookie

      public void GenerateCookie(HttpResponse response, string name, string token)
        {
            try
            {
                int timeToLiveCookie = 0;
                TimeSpan timeSpanToAdd = default(TimeSpan);
                DateTime currentDate = DateTime.Now;
                CookieOptions cookieOptions = new CookieOptions();
                cookieOptions.Path = "/";
                cookieOptions.HttpOnly = false;
    
                cookieOptions.Expires = currentDate.AddYears(10);
                response.Cookies.Append(name, token, cookieOptions);             
    
            }
            catch (Exception exception)
            {
                throw exception;
            }
        }
    

    【讨论】:

    • 感谢您的回复 alex,但我已经生成了一个 cookie。我希望在我拥有的 WS-Federation 设置中有一种更“官方”的方式来过期身份验证。我只是不明白生成另一个 cookie 对我有什么帮助。也许我在这里误解了什么?
    猜你喜欢
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2015-05-31
    • 2015-01-30
    • 1970-01-01
    • 2017-01-17
    • 2013-09-18
    相关资源
    最近更新 更多