Forms身份認(rèn)證在IE11下無法保存Cookie的問題
1. 網(wǎng)站根目錄下的Web.config添加authentication節(jié)點
<authentication mode="Forms">
<forms name="MyAuth" loginUrl="manager/Login.aspx" defaultUrl="manager/default.aspx" protection="All" timeout="60" />
</authentication>
2. 在manager子目錄下添加Web.config文件并加入下面的內(nèi)容:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow roles="Admin" />
<deny users="*" />
</authorization>
</system.web>
</configuration>
這樣,用戶在沒有Forms認(rèn)證的情況下訪問manager子目錄下的任何頁面均會自動跳轉(zhuǎn)到manager/Login.aspx頁面。如果認(rèn)證成功,則會默認(rèn)回到manager/default.aspx頁面。認(rèn)證有效期為60分鐘。
3. 添加認(rèn)證代碼。登錄按鈕中添加下面的代碼:
if (!snCheckCode.CheckSN(txt_ValidateCode.Text))
{
snCheckCode.Create();
Utility.ShowMessage("校驗碼錯誤!");
return;
}
string strUserName = txt_Username.Text.Trim();
string md5Pwd = Helper.MD5ForPHP(Helper.MD5ForPHP(txt_Password.Text));
lc_admin admin = null;
bool logined = false;
using (var context = new dbEntities())
{
admin = context.tb_admin.Where(n => n.username == strUserName).FirstOrDefault();
if (admin != null)
{
if (admin.checkadmin != "true")
{
snCheckCode.Create();
Utility.ShowMessage("抱歉,該賬號被禁止登錄!");
return;
}
if (admin.password == md5Pwd)
{
// Update Admin Info
admin.loginip = Request.UserHostAddress.ToString();
admin.logintime = CndingUtility.DateTimeToUnixTimeStamp(DateTime.Now);
context.SaveChanges();
logined = true;
}
}
}
if (logined)
{
// Login
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
admin.id.ToString(),
DateTime.Now,
DateTime.Now.AddMinutes(60),
false,
"Admin",
FormsAuthentication.FormsCookiePath
);
string hashTicket = FormsAuthentication.Encrypt(ticket);
HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);
HttpContext.Current.Response.Cookies.Add(userCookie);
if (Request["ReturnUrl"] != null)
{
Response.Redirect(HttpUtility.HtmlDecode(Request["ReturnUrl"]));
}
else
{
Response.Redirect("/manager/default.aspx");
}
}
else
{
snCheckCode.Create();
CndingUtility.ShowMessage("用戶名或密碼不正確!");
}
MD5加密代碼:
public static string MD5ForPHP(string stringToHash)
{
var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] emailBytes = Encoding.UTF8.GetBytes(stringToHash.ToLower());
byte[] hashedEmailBytes = md5.ComputeHash(emailBytes);
StringBuilder sb = new StringBuilder();
foreach (var b in hashedEmailBytes)
{
sb.Append(b.ToString("x2").ToLower());
}
return sb.ToString();
}
認(rèn)證成功后默認(rèn)會將用戶登錄信息以Cookie的形式存放到客戶端,有效期為60分鐘。UserData被設(shè)置為用戶的角色,在判斷用戶是否登錄時會用到。如下面的代碼:
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
int adminId = -1;
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
FormsAuthenticationTicket ticket = identity.Ticket;
string userData = ticket.UserData;
if (userData == "Admin")
{
// To do something
}
}
上述代碼在Visual Studio中運(yùn)行一切正常!但是將網(wǎng)站發(fā)布到服務(wù)器的IIS (可能會是較低版本的IIS,如IIS 6)后,發(fā)現(xiàn)登錄功能異常。輸入用戶名和密碼后點擊登錄按鈕,頁面postback但并不能正確跳轉(zhuǎn),嘗試手動訪問受保護(hù)的頁面會被自動跳轉(zhuǎn)回登錄頁面。更奇怪的是該問題只出現(xiàn)在IE11瀏覽器上,嘗試用Firefox或Chrome訪問登錄功能運(yùn)行正常。初步懷疑是IIS設(shè)置的問題,可是IIS 6上并沒有與Cookie相關(guān)的設(shè)置,好像記得IIS 7上倒是有這個設(shè)置。但因為只有IE 11存在該問題,所以可以否定代碼本身存在任何問題。
此外,還嘗試了降低IE 11的安全級別,重新安裝服務(wù)器上的.net framework以及下載最新的補(bǔ)丁等等,均不能解決問題。后來發(fā)現(xiàn)其實只需要簡單修改Web.config中authentication節(jié)點的設(shè)置就可以了,給forms添加cookieless="UseCookies"屬性即可。
<authentication mode="Forms">
<forms name="MyAuth" cookieless="UseCookies" loginUrl="manager/Login.aspx" defaultUrl="manager/default.aspx" protection="All" timeout="60" />
</authentication>
用以明確告訴服務(wù)器使用Cookie來保存用戶驗證信息。問題解決!
相關(guān)文章
jquery.pagination +JSON 動態(tài)無刷新分頁實現(xiàn)代碼
jquery.pagination +JSON 動態(tài)無刷新分頁實現(xiàn)代碼,需要的朋友可以參考下。2011-12-12asp.net錯誤處理Application_Error事件示例
Application_Error事件與Page_Error事件相類似,可使用他捕獲發(fā)生在應(yīng)用程序中的錯誤。由于事件發(fā)生在整個應(yīng)用程序范圍內(nèi),因此您可記錄應(yīng)用程序的錯誤信息或處理其他可能發(fā)生的應(yīng)用程序級別的錯誤2014-01-01記Asp.Net Core Swagger使用并帶域接口處理的方法
這篇文章主要介紹了記Asp.Net Core Swagger使用并帶域接口處理的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03asp.net System.Net.Mail 發(fā)送郵件
一個師弟發(fā)了段代碼給我,說調(diào)試了很久發(fā)送郵件都沒有成功。自己使用過程中,也發(fā)現(xiàn)了很多問題,但最簡單的問題是“發(fā)件方”地址根本不支持smtp發(fā)送郵件。2009-04-04Asp.net mvc 權(quán)限過濾和單點登錄(禁止重復(fù)登錄)
這篇文章主要介紹了Asp.net mvc 權(quán)限過濾和單點登錄(禁止重復(fù)登錄)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12利用ASP.NET MVC和Bootstrap快速搭建個人博客之文章打賞功能(六)
這篇文章主要介紹了利用ASP.NET MVC和Bootstrap快速搭建個人博客之文章打賞功能(六) 的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-07-07asp.net動態(tài)產(chǎn)生checkbox(數(shù)據(jù)源為DB或內(nèi)存集合)
動態(tài)產(chǎn)生一組checkbox(數(shù)據(jù)源為DB或內(nèi)存集合)且post提交時后臺能及時獲取等等,打算使用repeater+input(checkbox)+input(hidden)來實現(xiàn)2013-10-10ASP.NET筆記之 圖庫權(quán)限設(shè)置的方法
本篇文章小編為大家介紹,ASP.NET筆記之 圖庫權(quán)限設(shè)置的方法。需要的朋友參考下2013-04-04關(guān)于 嘗試讀取或?qū)懭胧鼙Wo(hù)的內(nèi)存。這通常指示其他內(nèi)存已損壞。的解決方法
這篇文章主要介紹了嘗試讀取或?qū)懭胧鼙Wo(hù)的內(nèi)存。這通常指示其他內(nèi)存已損壞。的解決方法,有需要的朋友可以參考一下2013-12-12