這將是我的第一個問題!使用MVC4隨機註銷asp.net
我的mvc4應用程序和隨機發生的註銷有問題。
我使用會話來存儲我的公司ID和用戶的ID。
private void SetSessionData(string UserName)
{
Employee data = (from employee in _db.Employees where employee.Email == UserName select employee).First();
Session.Add("Comp_ID", data.Comp_ID);
Session.Add("Company", data.Company.Name);
Session.Add("User_ID", data.ID);
}
我已經設置了超時值以600爲會話(10小時),這甚至設置2個地方可以肯定的:
[AllowAnonymous]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
//FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); //sørger for at remember me virker!
SetSessionData(model.UserName);
Session.Timeout = 600;
if (model.RememberMe)
{
Response.Cookies.Add(new HttpCookie("CookieUserName", model.UserName) { Expires = DateTime.Now.AddDays(30), Value = model.UserName });
Response.Cookies.Add(new HttpCookie("CookieRememberMe", model.RememberMe.ToString()) { Expires = DateTime.Now.AddDays(30), Value = model.RememberMe.ToString() });//sætter den nye cookie
}
else
{
Response.Cookies.Set(new HttpCookie("CookieUserName") { Expires = DateTime.Now.AddDays(-1) });
Response.Cookies.Set(new HttpCookie("CookieRememberMe") { Expires = DateTime.Now.AddDays(-1) });
}
if (string.IsNullOrEmpty(returnUrl))
{
return RedirectToLocal(returnUrl);
}
return RedirectToAction("Index", "Home");
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "Vi har enten ikke brugernavnet eller koden i kartoteket.");
return View(model);
}
這裏在web.config:
<system.web>
<machineKey validationKey="MyKeyGoesHere" validation="SHA1" decryption="AES" />
<sessionState timeout="600" />
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
<httpRuntime targetFramework="4.5" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="600" />
</authentication>
我的cookies似乎被保存了10個小時,並且我的session_id cookie過期似乎被設置爲「當瀏覽器關閉時」。
服務器端我已設置應用程序池以在凌晨1點回收。
即使所有這些設置,我的用戶仍然會在登錄後2分鐘到登錄後1小時之間獲得隨機註銷。
對付一些隨機半登錄狀態的問題我有我這包括:
@if(Session == null || Session["User_ID"] == null || !WebSecurity.Initialized){
//Makes sure the session data is cleared and user logged out if session dies.
try
{
if(Session != null) {Session.Clear();}
if (WebSecurity.Initialized){WebSecurity.Logout();}
FormsAuthentication.SignOut();
//dette er til at stoppe cache.
Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}catch{ <p>Error Clearing Login Cache</p>}
}
Im相當現在失去了,希望一個大師那裏可能知道初學者的錯誤我在這裏做什麼!
感謝您提前給我的回覆!
編輯:
我也試過這個:http://www.windowsitpro.com/article/security-development/create-persistent-id-cookies
(從原文鏈接:ASP.NET MVC FormsAuthentication Cookie timeout cannot be increased)
但只是做每一次我登錄後,按什麼我的應用程序註銷。
該應用程序運行在IIS8的Windows 2012服務器上。
更補充說:
我發現了SESSION_ID的cookie仍設置當在瀏覽器關閉:
cloud.hviidnet.com/image/2X3v2y2e1K1S
奇怪的是它的集600分鐘,甚至當我看在IIS服務器:
cloud.hviidnet.com/image/1e3J1g2u3p2M
我以前有過類似的問題;我檢查過我的配置文件,我的'machineKey validationKey'元素有一個'decryptionKey'屬性,你不知道。您可能想要移除整個元素,因爲如果您是自主託管,它可能不是必需的。看到這個鏈接http://blog.scribz.net/2011/03/preserve-aspx-session-postback-alive-hosted/ – wal
@wal - 感謝您的回覆。我添加了machineKey部分,因爲它在stackoverflow上解決了類似的問題。你沒錯,我似乎沒有解密密鑰。我已經產生了一個新的,看起來像這樣:<的machineKey 的validationKey =「57BEE8DA71A66493A7D8366B79728691F9D4E85AABFAD9AA2B4FCB05A87702100F4DEEE848525B74C7E3084AA2F38A13585B3C204A4287825D149E4F7BE34B4D」 decryptionKey =「E2146D73483F1028AE3C738B1100DEB0DD8BD105886DC8BF814AD19A853FF569」 驗證=「SHA1」解密=「AES」 /> 我會後回來,如果它的解密工作標記已啓用。 – Wraithbone