2014-01-25 63 views
0

我正在使用默認的MVC 5項目,我保護以下方法,以便只有用戶以「foobar」登錄才能訪問它。這種特殊的用戶將存在正常的數據庫之外(和在web.config中定義,但現在它是硬有線)如何手動創建身份並在MVC中對其進行身份驗證?

[Authorize(Users = "foobar")] 
    public ActionResult Install() 
    { 
     //setup database; 
     return View(); 
    } 

而且我檢查foobar的登錄方法,如果檢測,我想登錄它們,然後將它們重定向到安裝操作。

public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
    { 
     if (model.UserName.ToLower().Trim() == "foobar" && model.Password == "pass") 
       { 
        //create an Idenity named foobar and log them in 
        return RedirectToAction("Install"); 
+0

只是好奇,爲什麼你要創建一個正常的數據庫之外的硬編碼密碼的特殊用戶?默認用戶存儲(密碼散列)中內置了許多保護措施,您不會在此一次性帳戶中使用這些保護措施。 – 0leg

回答

-1
 string username = model.username; 
     string password = model.password; 

     // This is where you do your super user check against the config file 
     bool userValid = true; // compare id and password against config. 
     // this is a bad idea, back door user is a security hole that can be exploited 

     if (userValid) 
     { 
      //this will set the identity and the cookie for your user 
      FormsAuthentication.SetAuthCookie(username, false); 
     } 
相關問題