SignalR和ASP.NET Identity的ExpireTimeSpan

4
我正在使用基于Cookie的认证与ASP.NET Identity。 我将ExpireTimeSpan设置在CookieAuthenticationOptions类中,以控制在用户必须重新登录之前允许多少时间不活动。 这一切都很好运行,但当我向应用程序添加SignalR时,在一段不活动期后,用户不再必须重新登录。 SignalR定期进行“ping”请求,我认为正是这个导致了cookie过期时间的延长。 我正在寻找一种方法来不更新SignalR URL的cookie到期时间。 我已经研究了Microsoft.Owin.Security.Cookies中的一些代码,特别是CookieAuthenticationHandler类中的AuthenticateCoreAsync方法。 这个方法中有逻辑来决定是否更新cookie。 然而,CookieAuthenticationHandler类是内部的,所以我无法覆盖此方法。 请问是否有可用的钩子来实现此功能?

你弄清楚了吗? - silvio
抱歉,我尝试在一个HTTP模块中屏蔽Signal R请求的cookies,但没有成功。 - DownChapel
1个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
0
我们在公司解决了这个问题,通过使用 HttpModule 从 signalr 响应中删除 cookies。
public class NoFormsAuthenticationModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.PreSendRequestHeaders += OnPreSendRequestHeaders;
    }

    protected void OnPreSendRequestHeaders(object sender, EventArgs e)
    {
        var httpContext = ((HttpApplication)sender).Context;

        var path = httpContext.Request.Path;

        var noAuthentUrls = new string[] { "/signalr/" };

        foreach (var url in noAuthentUrls)
        {
            var noAuthentication = path.IndexOf(url, StringComparison.OrdinalIgnoreCase) > -1;

            if (noAuthentication)
                httpContext.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
        }            
    }

}
希望能对您有所帮助。 不要忘记在web.config中添加条目:

< system.web>
< httpModules> < add name="NoFormsAuthenticationModule" type="Site.Components.HttpModules.NoFormsAuthenticationModule"/>

< system.webServer> < modules runAllManagedModulesForAllRequests="true">
< add name="NoFormsAuthenticationModule" type="Site.Components.HttpModules.NoFormsAuthenticationModule"/>

...


谢谢!我会尝试一下。 - DownChapel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,