asp.net-mvc-3 – 超时在ASP.Net MVC FormsAuthentication中不起作用
|
我正在使用FormsAuthentication,我在设置TimeOut值时遇到问题. 我看过一些与此有关的其他帖子,但它们似乎并不完全符合我的问题,或者解决方案没有帮助. 我的web.config具有以下内容: <authentication mode="Forms">
<forms loginUrl="~/Account/LogOn"
timeout="1"
cookieless="UseCookies" />
</authentication>
我把一个AuthorizeAttribute放在我想要保护的控制器上. 我可以查看.ASPXAUTH cookie(使用FireCookies),我可以看到它设置为在登录后1分钟内到期. 如果我调试我的代码,我可以看到FormsAuthentication.Timeout = 1. 不过我的门票在1分钟内似乎没有超时. 1分钟不活动后,我仍然可以使用AuthorizeAttribute浏览到控制器. 实际上我实际上可以使用FireCookie删除.ASPXAUTH cookie,我仍然可以使用AuthorizeAttribute浏览到控制器. 在很长一段时间后,很奇怪的(抱歉没有一个确切的时间 – 我出去吃午饭了)TimeOut发生了,我被重定向 有任何想法吗? 解决方法我也有同样的问题.其实呢,是因为我无法从javascript读取表单认证cookie,这是一段时间未定义的.我只想知道我是否通过javascript进行身份验证.后来我发现这张票已经过期,但我没有注销(也是,所以我也想解决)!我看到你的问题没有得到回答,所以当我解决问题半天的时候,我保持开放.以下是我想出的,似乎是工作. 我的答案是基于这个答案. https://stackoverflow.com/a/454639/511438由用户ScottS 这是在我的ASP.NET MVC 3项目. 这是我的登录代码.未显示,自定义用户认证逻辑之前.这只是设置初始票. public class FormsAuthenticationService:IFormsAuthentication public void SignIn(string userName,bool createPersistentCookie,string role)
{
FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
1,// version
userName,// user name
DateTime.Now,// created
DateTime.Now.Add(FormsAuthentication.Timeout),// expires
false,// rememberMe?
role // can be used to store roles
);
string encryptedTicket = FormsAuthentication.Encrypt(authTicket);
HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,encryptedTicket);
HttpContext.Current.Response.Cookies.Add(authCookie);
}
在同一个类中,但是从global.asax访问的静态方法 //-- this is based on https://stackoverflow.com/questions/454616/asp-net-cookies-authentication-and-session-timeouts
internal static FormsAuthenticationTicket RefreshLoginCookie(bool retainCurrentExpiry)
{
HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == null)
return null;
FormsAuthenticationTicket oldTicket = FormsAuthentication.Decrypt(authCookie.Value);
DateTime expiryDate = (retainCurrentExpiry ? oldTicket.Expiration : DateTime.Now.Add(FormsAuthentication.Timeout));
HttpContext.Current.Response.Cookies.Remove(FormsAuthentication.FormsCookieName);
var newTicket = new FormsAuthenticationTicket(oldTicket.Version,oldTicket.Name,oldTicket.IssueDate,expiryDate,oldTicket.IsPersistent,oldTicket.UserData,oldTicket.CookiePath);
HttpCookie newAuthCookie = new HttpCookie(FormsAuthentication.FormsCookieName,FormsAuthentication.Encrypt(newTicket));
HttpContext.Current.Response.Cookies.Add(newAuthCookie);
return newTicket;
}
Global.asax中 我的自定义是ajax请求不刷新表单身份验证票证.所以如果你坐在那里的超时时间,一个ajax请求将会注销你.更改,如果你想要ajax请求保持机票活着(解决我的JavaScript cookie问题,而不是你的注销不活动问题). protected virtual void Application_AuthenticateRequest(Object sender,EventArgs e)
{
HttpCookie authCookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (authCookie == null || authCookie.Value == "")
{
return;
}
bool isAjax = new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest();
FormsAuthenticationTicket authTicket;
try
{
//-- THIS IS WHAT YOU WANT
authTicket = FormsAuthenticationService.RefreshLoginCookie(isAjax);
}
catch
{
return;
}
string[] roles = authTicket.UserData.Split(';');
if (Context.User != null) Context.User = new GenericPrincipal(Context.User.Identity,roles);
}
Web.config文件 <configuration>
<system.web>
<sessionState mode="InProc" timeout="60" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="60" name="ProviderMvcSession" cookieless="UseCookies" />
</authentication> (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net-mvc-4 – visual studio 2012 RC无法加载类型Syste
- 具有匹配客户端和服务器端标记的ASP.NET页面的选项?
- asp.net – 脚本管理器控件实际上是做什么的?
- asp.net mvc ajax上传解决方案?
- asp.net-mvc – 如何使用绑定前缀?
- 从旧的经典ASP页面设置301重定向到新的ASP.NET Webforms页面
- asp.net-mvc – ASP.NET MVC / EF4 / POCO /存储库 – 如何
- asp.net-mvc – 使用ASP.NET MVC的IIS应用程序请求路由(ARR
- asp.net-mvc – 如何使用Moq测试一个自定义的ModelBinder?
- asp.net – 将数值强制为HTML表导出为excel的文本
- asp.net-mvc – ASP.NET MVC:部分知道它是否是从
- asp.net会员 – 自动生成的密钥不支持散列或加密
- 如何将成员资格表与实体框架集成? Asp.net
- .net – 使用log4net和在哪里实现它并使用elmah?
- asp.net-mvc – 尝试创建类型为’TypeNewsContro
- asp.net – 如何绑定gridview中的下拉列表?
- ASP .NET MVC Forms授权与Active Directory组
- asp.net-mvc-3 – 在MVC Razor View中使用If语句
- ASP.NET Webforms验证框架的建议
- 如何阻止MVC缓存调用动作方法的结果?
