【问题标题】:ASP.NET - owin static WebMethod - cookie not validatedASP.NET - owin 静态 WebMethod - cookie 未验证
【发布时间】:2016-11-23 16:52:40
【问题描述】:

我的 owin 设置为使用基于 cookie 的身份验证和 OpenIdConnect,如下所示:

        var options = new CookieAuthenticationOptions()
        {
            ExpireTimeSpan = TimeSpan.FromDays(30),
            Provider = new CookieAuthenticationProvider {
               OnValidateIdentity = cookieValidateIdentityContext =>
               {
                cookieValidateIdentityContext.Properties.ExpiresUtc = DateTime.UtcNow.AddMinutes(1);
                return Task.FromResult(30);
               }
            }
        }
        app.UseCookieAuthentication(options);
        app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions() {.....})

我有一个像这样的静态 WebMethod

    [WebMethod]
    public static String GetJsonData(string query)
    {
        var user = HttpContext.Current.User.Identity.Name;
        .....
    }

这工作正常,但如果用户离开浏览器超过 20 分钟,身份验证失败。 HttpContext.Current.User.Identity.IsAuthenticated 为假。

即使我的 cookie 设置为 30 天后过期,也会发生这种情况。有什么方法可以强制验证 cookie 并进行身份验证?我没有使用基于表单的身份验证。

另外,一旦超过 20 分钟,cookieValidateIdentityContext 函数就不会被调用。如果我在 cookieValidateIdentityContext 中设置断点,在 20 分钟之前,断点被命中,但不是之后。

【问题讨论】:

    标签: asp.net asp.net-mvc cookies owin


    【解决方案1】:

    根据这篇文章Cookie MiddlewareSlidingExpiration 导致问题尝试在 web.config 中设置它。

    SlidingExpiration - 一个标志,指示 cookie 到期日期是否会在超过一半的 ExpireTimeSpan 间隔已过时重置。新的到期日期将向前移动为当前日期加上 ExpireTimespan。在调用 SignInAsync 时,可以使用 AuthenticationProperties 类设置绝对到期时间。绝对过期可以通过限制身份验证 cookie 的有效时间来提高应用程序的安全性。

    【讨论】:

    • 看来 SlidingExpiration 只能用于基于表单的身份验证。我在这里使用owin
    • 我提供的链接是给 OWIN 的
    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 2015-10-09
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2017-10-22
    相关资源
    最近更新 更多