ASP.NET_FORM表單驗證_單點登錄_Nomads (FormsAuthentication)

http://apps.hi.baidu.com/share/detail/21099688

一: ASP.NET 的安全認證模式
Windows, Forms, Passport, None

二: 修改驗證模式
修改Web.config <system.web>

    <!--修改驗證模式爲Forms-->
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" name="HotelUser" defaultUrl="Default.aspx"></forms>
</authentication>
<!--禁止匿名登陸-->
<authorization>
<deny users="?"/>
</authorization>

三: 用戶登陸,發身份驗證票
FormsAuthentication.FormsCookieName ~ 就是上面的HotelUser

//登陸成功後,返回請求頁面
a:System.Web.Security.FormsAuthentication.RedirectFromLoginPage(FormsAuthentication.FormsCookieName,false); 

//發驗證票,到指定頁面
b:System.Web.Security.FormsAuthentication.SetAuthCookie(FormsAuthentication.FormsCookieName,false); 
Response.Redirect("Default.aspx");

四: 用戶退出
System.Web.Security.FormsAuthentication.SignOut();

五: 用戶是否通過驗證
User.Identity.IsAuthenticated //如果通過驗證,或是有Cookie存在 值爲True,否則爲False

六: Web.config 作用範圍
0: machine.config 的設置 作用整個機器所有目錄及其目錄下所有文件.   -->子隨父姓
1: Web.config 的設置 作用於所在目錄的所有文件及其子目錄下所有文.   -->子隨父姓
2: 子目錄下的 Web.config 設置將覆蓋由父目錄繼承下來的設置          -->將在外,軍命有所不受

七: 設置某個文件夾或文件的訪問權限
1:在相應文件夾下 建立web.config文件 
<authorization>
<deny users="?"/>   //這裏設置訪問權限
</authorization>

2: 在根目錄web.config下設置整個站點 所有文件夾 和 文夾的訪問權限

<configuration>
//目錄文件夾1
<location path ="Public">    //<location path ="Public/Default.aspx"> 爲某個文件設置訪問配置
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>

   //目錄文件夾2
<location path ="ManageSys">
<system.web>
<authorization>
<allow users="Admin"/>
<allow users="WF"/> 
<allow users="FY"/> 
<deny users="*"/>         
</authorization>
</system.web>
</location>

//原根目錄web.config 配置
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Login.aspx" name="HotelUser" defaultUrl="Default.aspx"></forms>
</authentication>    
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>

八:單點登陸
1:獲取機器key 生成密鑰

<密鑰生成方法>
protected void btn_OK_Click(object sender, EventArgs e)
{
string decStr = this.CreateKeyString(int.Parse(this.TextBox1.Text));
string valStr = this.CreateKeyString(int.Parse(this.TextBox2.Text));
this.TextBox3.Text = string.Format("<machineKey validationKey=\"{0}\" decryptionKey=\"{1}\" validation=\"SHA1\"/>", valStr, decStr);
}
/// <summary>
/// 生成加密型強隨機 Key 值
/// </summary>
/// <param name="i">Key 的有效長度:
/// decryptionKey 的有效值爲 8 或 24;
/// validationKay 的有效值爲 20 至 64
/// </param>   
private string CreateKeyString(int i)
{
System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider(); //加密隨機數生成器
byte[] bt = new byte[i];
rng.GetBytes(bt);//用加密型強隨機值序列填充字節數組
System.Text.StringBuilder str = new System.Text.StringBuilder();
for (int j = 0; j < i; j++)
{
str.Append(string.Format("{0:X2}", bt[j])); //轉換成大寫的十六進制文本
}
return str.ToString();
}
2:在要實現單點登陸的項目根web.config中添加密鑰;

a) 兩個項目Web.cinfig的<machineKey> 節點確保以下幾個字段完全一樣:validationKey 、decryptionKey 、validation 
b) 兩個項目的 Cookie 名稱必須相同,也就是 <forms> 中的 name 屬性,這裏我們把它統一爲 name ="UserLogin"    //name="www.wf.com"
c) 注意區分大小寫 
d) 登陸頁面整合到統一登陸點登陸   比如:loginUrl="www.wf.com/login.aspx", 在登陸頁面發放驗證票     


//項目網站1
<system.web>
<machineKey validationKey="B3D04DE25D02B63898851BD799F5E8DBE7CE043B9A09AC2E5ACE14BD9C84717E3731837A76923B327340945010C58C31" decryptionKey="9EBAA26A9E9424994CE2C0A4C0EA5B20" validation="SHA1"/>   
<authentication mode="Forms">
<forms name="UserLogin" loginUrl="~/Login.aspx"></forms>
</authentication>
<authorization>
<deny users ="?"/>
</authorization>   
</system.web>

//項目網站2
<system.web>
<machineKey validationKey="B3D04DE25D02B63898851BD799F5E8DBE7CE043B9A09AC2E5ACE14BD9C84717E3731837A76923B327340945010C58C31" decryptionKey="9EBAA26A9E9424994CE2C0A4C0EA5B20" validation="SHA1"/>   
<authentication mode="Forms">
<forms name="UserLogin" loginUrl="~/Login.aspx"></forms>
</authentication>
<authorization>
<deny users ="?"/>
</authorization>   
</system.web>

      3: 給用戶發放Cookie 
<1>: 一次登陸後,給各個站點同時發放Cookie認證。
<2>: 一次登陸後,根據用戶選擇性的發放Cookie認證。

九: Cookie
1: 普通Cookie
protected void Button1_Click(object sender, EventArgs e)
{
HttpCookie ck = new HttpCookie("str");
ck.Expires.AddDays(1);
ck["rr"] = "str_rr_";
ck["w1"] = "str_w1_";       
Response.Cookies.Add(ck);  

HttpCookie ckNex = new HttpCookie("Nex");
ck.Expires.AddDays(1);        
ck.value= "Nex_";       
Response.Cookies.Add(ckNex);         
}
protected void Button2_Click(object sender, EventArgs e)
{          
TextBox1.Text = Request.Cookies["str"]["w1"].ToString() + Request.Cookies["str"]["rr"].ToString();        
}

2: 生成用戶驗證的Cookie     
public void AuthenticationUsers(string userName)
{
FormsAuthenticationTicket tichet = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddHours(24), true, "");
string hashTichet = FormsAuthentication.Encrypt(tichet);

            HttpCookie userCookie = new HttpCookie(FormsAuthentication.FormsCookieName);
userCookie.Value = hashTichet;
userCookie.Expires = tichet.Expiration;
userCookie.Domain = FormsAuthentication.CookieDomain;
HttpContext.Current.Response.Cookies.Add(userCookie);
}

這個Cookie 相當與 下面兩發放的Cookie

//登陸成功後,返回請求頁面
a:System.Web.Security.FormsAuthentication.RedirectFromLoginPage(FormsAuthentication.FormsCookieName,false); 
//發驗證票,到指定頁面
b:System.Web.Security.FormsAuthentication.SetAuthCookie(FormsAuthentication.FormsCookieName,false);


發佈了5 篇原創文章 · 獲贊 23 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章