2014-12-01 85 views
0

我正在製作一個使用MVC的個人項目,並希望實現身份驗證功能。閱讀一些書籍,谷歌搜索,它「出現」,有很多方式來正確認證用戶,包括:與表單身份驗證相混淆

  • 內置ASP.Net會員提供 - 我想避免這種情況。
  • 使用Cookie
  • 自定義窗體身份驗證

我不知道爲什麼,但使用cookie似乎只是老我的腦海裏自動認爲它不安全。我已經鏈接了一些我一直在閱讀的文章來幫助我。

我的過程中我自己的數據庫來驗證的基本認識是:

  • 用戶呼叫帳控制器,它允許匿名訪問(屬性),用戶輸入
  • 查詢數據庫(我使用實體框架)
  • 如果用戶找到了,那麼FormsAuthentication.SetAuthCookie(username,false);否則顯示錯誤。
  • 掛鉤高達的FormsAuthentication.OnAuthenticate方法,做下面的代碼項目的文章中類似的喜歡的東西:

代碼:

protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e) 
{ 
    if (FormsAuthentication.CookiesSupported == true) 
    { 
     if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) 
     { 

      try 
      { 
       //let us take out the username now     
       string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name; 
       string roles = string.Empty; 

       using (userDbEntities entities = new userDbEntities()) 
       { 
        User user = entities.Users.SingleOrDefault(u => u.username == username); 

        roles = user.Roles; 
       } 
       //let us extract the roles from our own custom cookie 


       //Let us set the Pricipal with our user specific details 
       e.User = new System.Security.Principal.GenericPrincipal(
        new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';')); 
      } 
      catch (Exception) 
      { 
       //somehting went wrong 
      } 
     } 
    } 
} 

擱筆:

formsauthentication.signout(). 

這是一個簡單但SECURE認證的盒子標準方式?我並不太擔心角色。最後,如果我使用授權屬性或將不得不使我自己的屬性這個上述工作。如果上述內容不正確或不安全,請告訴我需要什麼。我不想使用成員資格提供程序數據庫。我想用我自己的數據庫來驗證用戶。

更新。多閱讀一次後,它看起來像我還需要設置身份驗證票證。雖然我不知道爲什麼。

See the article on CodeProject

謝謝加里我修復了這個鏈接。

回答