2017-03-18 52 views
0

我創建了一個必須使用MVC模式完成的學校應用程序。首先,我試圖獲得一個簡單的註冊和登錄頁面,並運行,就好像我可以在我的數據庫中存儲和檢索一樣,然後應用程序的其餘部分應該相當簡單。我正在使用我的大學的SQL服務器作爲我的數據庫並使用ADO.NET實體數據模型(第一次)。ASP.NET MVC簡單的登錄頁面,用戶名和密碼匹配,但獲取未經授權的消息

我正在使用一個在線教程來幫助我,它似乎工作得不錯,但現在的麻煩是,當試圖使用我的表中的細節已登錄時,我得到一個'Http 401.0 - 未經授權'頁面。

我知道我在電子郵件&密碼框中輸入的內容與我在數據庫中檢索到的內容匹配,因爲我使用標籤進行了測試,並且它們都匹配。如果有幫助,我正在使用ASP.NET 4.5.2。

Unauthorized page

我的表是簡單 -

Email (pk)(varchar) - For logon 
Password (varchar) - For logon 
First_Name (varchar) 

我的代碼如下;

UserLogon視圖 -

public class UserLogon 
{  
    [Required] 
    [Display(Name = "Email")] 
    public string Email { get; set; } 

    [Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Password")] 
    public string Password { get; set; } 
} 

用戶管理器視圖 -

public class UserManager 
{ 
    private ac9555fEntities dre = new ac9555fEntities(); 
    public string GetUserPassword(string userEmail) 
    { 
     var user = from o in dre.UserTables where o.Email == userEmail select o; 
     if (user.ToList().Count > 0) 
      return user.First().Password; 
     else 
      return string.Empty; 
    } 
} 

賬戶控制器 -

public class AccountController : Controller 
{ 

    public ActionResult LogOn() 
    { 
     return View(); 
    } 

    // 
    // POST: /Account/LogOn 

    [HttpPost] 
    public ActionResult LogOn(UserLogon model, string returnUrl) 
    { 

     if (ModelState.IsValid) 
     { 

      UserManager userManager = new UserManager(); 
      string password = userManager.GetUserPassword(model.Email); 


      if (string.IsNullOrEmpty(password)) 
      { 

       ModelState.AddModelError("", "The user login or password provided is incorrect."); 
      } 

      if (password == model.Password) 
      { 

       FormsAuthentication.SetAuthCookie(model.Email, false); 
       if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") 
        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) 
       { 
        return Redirect(returnUrl); 
       } 
       else 
       { 
        return RedirectToAction("Welcome", "Home"); 
       } 
      } 
      else 
      { 
       ModelState.AddModelError("", "The password provided is incorrect."); 
      } 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 
    } 

首頁控制器 -

public ActionResult Index() 
    {   
     return View(); 
    } 

    [Authorize] 
    public ActionResult Welcome() 
    { 
     return View(); 
    } 

LogOn支持視圖 -

@model WebApplication5.Models.ViewModels.UserLogon 
.... 
@using (Html.BeginForm()) 
{ 
    .... 
    @Html.EditorFor(model => model.Email) 
    .... 
    @Html.EditorFor(model => model.Password) 
    .... 
    <input type="submit" value="Log On" /> 
} 

如果我已經包含了太多的/沒有足夠的代碼我不道歉,這是一個使用MVC/ADO.NET實體數據模型我的第一次。從在這裏和其他網站上查看其他內容,我覺得有一個額外的授權層不是必需的,但我嘗試過的任何解決方案都失敗了。任何人都可以發現我的代碼有缺陷嗎?

<system.web> 
<compilation debug="true" targetFramework="4.5.2" /> 
<httpRuntime targetFramework="4.5.2" /> 
</system.web> 
+0

你從哪裏得到那個錯誤? –

+0

在瀏覽器中,那是什麼意思? @StephenMuecke – ACostea

+0

什麼行代碼導致它。 –

回答

1

的錯誤發生,因爲您的weg.config文件DOS不指定要使用窗體身份驗證。修改<system.web>部分以包含

<system.web> 
    .... 
    <authentication mode="Forms"> 
     <forms loginUrl="~/Account/LogOn" timeout="10" /> // adjust as required 
    </authentication> 
</system.web> 

然而,您的代碼還存在其他一些問題。

  • 你不應該密碼存儲在數據庫中以純文本(他們 需要進行散列和醃製)
  • 你的第二個ModelState.AddModelError(..)代碼應該是相同 到第一(從來沒有透露這兩人的值不正確)
  • 其真不明白你的努力與您if(Url.IsLocalUrl(returnUrl) && ...代碼,這樣做,但一旦你驗證 用戶,它應該只是if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); } else { return RedirectToAction("Welcome", "Home"); }

我建議您通過Security, Authentication and Authorization的基本教程進行學習,並使用MVC和Identity的內置功能來正確處理所有這些問題。

相關問題