ASP.NET Form表單驗證

來源:互聯網
上載者:User

一: 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個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.