ASP.NET身分識別驗證機制membership入門——API篇

來源:互聯網
上載者:User

  不知道什麼原因,最近總是頭暈腦脹,做什麼事情感覺都不在狀態,再加上工作忙,覺得好累~不過不管怎麼說,寫部落格一定要堅持,最少一月一篇~這算是給自己的任務吧。

  回到正題,前面說過如何去配置Membership,以及如何去使用Asp.net內建的登陸控制項。這次說明一下如何不使用Asp.net內建的登陸控制項,而通過編寫代碼的方式去完成相應的功能。

  要使用Membership我們需要引入System.Web.Security這個命名空間。在這個命名空間中有這麼幾個常用的類:

  1. Membership
  2. Roles

 

  首先來介紹第一個Membership類,Membership類是一個靜態類,提供了所有的對於使用者方面的操作,比如註冊使用者,刪除使用者,取得使用者列表,根據email尋找使用者等等

  方法很多,但是大多數都很簡單,我們挑兩個比較麻煩點的來詳細解釋一下:

  Membership.CreateUser方法的功能,看名字就知道是建立使用者,這個方法有4個重載:

 

public static MembershipUser CreateUser(string username, string password);

public static MembershipUser CreateUser(string username, string password, string email);

public static MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, out MembershipCreateStatus status);

public static MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status);

 

  這4個重載的傳回值都是MembershipUser類,MembershipUser類其實就是一個實體類,不過裡邊帶了一些方法,比如取得密碼,修改密碼等方法。至於該使用哪個重載,還記得前面的web.config配置嗎?比如requiresQuestionAndAnswer設定是否要求輸入密碼找回問題和答案,如果該設定為true,那麼就得使用第三個,或者第四個重載,提供問題和答案了。isApproved這個參數表示建立的使用者是否啟用。最後一個MembershipCreateStatus 是一個枚舉,用out關鍵字修飾,用來獲得建立使用者的狀態,比如:MembershipCreateStatus.Success說明建立使用者成功,MembershipCreateStatus.InvalidEmail說明電子郵件的格式錯誤等等。providerUserKey是一個Guid類型的資料,用來指定該使用者的UserId。

 

  另一個需要介紹的就是public static bool ValidateUser(string username, string password);這個方法,其實看名字和參數大家都應該猜到這個方法的作用了,兩個參數分別是使用者名稱和密碼,傳回值為是否登入成功。剩下的方法都非常的簡單了。大家應該一看就明白,不過需要提一下的是:ValidateUser方法經常和FormsAuthentication類配合起來使用,FormsAuthentication類提供了一系列靜態方法,用來管理Forms驗證服務。

  FormsAuthentication類中,常用方法有:

public static string HashPasswordForStoringInConfigFile(string password, string passwordFormat);public static void SetAuthCookie(string userName, bool createPersistentCookie);
public static void RedirectFromLoginPage(string userName, bool createPersistentCookie);

 

  第一個HashPasswordForStoringInConfigFile方法,作用很簡單,將傳入的password按照passwordFormat指定的雜湊演算法產生雜湊密碼然後返回,passwordFormat的取值可以是"MD5”或是"SHA1"這兩個選擇。

  第二個SetAuthCookie方法,向用戶端發送身分識別驗證的票據。userName是登陸的使用者名稱,createPersistentCookie用來告訴Asp.net是否建立一個持久的Cookie,讓使用者下次訪問能夠免於登陸。

  第三個RedirectFormLoginPage方法,將經過身分識別驗證的使用者重新導向回最初請求的 URL 或預設 URL。

  通常就是這樣的用法:

 

if (Membership.ValidateUser("zhangsan", "123123"))
{
    FormsAuthentication.RedirectFromLoginPage("zhangsan", false);
}

 

 

  Roles類同樣是一個靜態類,裡邊封裝了所有對於角色相關的方法。比如:建立角色、將使用者添加到角色,刪除角色等常用操作。使用Roles類要注意:web.config配置中roleManager節必須正確配置,並且enabled屬性必須等於true才可以使用。

  Roles類中,需要說一下的是AddUserToRole這個方法,這個方法通常和Membership.CreateUser在一起使用,比如:

if (Membership.CreateUser("zhangsan", "123123"))
{
    //建立使用者成功,給zhangsan分配User角色
    Roles.AddUserToRole("zhangsan", "User");
}

 

  這樣,在建立zhangsan這個使用者之後,順便給zhangsan分配了User這個角色。在後面,就可以再設定檔中做如下配置:

<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
      <authorization>
        <allow roles="User"/>
        <deny users="?"/>
      </authorization>
    </system.web>
</configuration>

  這樣,所有新註冊的成員才可以訪問本目錄下的資源,具體配置可以參考ASP.NET身分識別驗證機制membership入門——配置篇(2)。

 

  ok.API的部分就介紹到這裡了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.