2016-07-29 143 views
1

使用ASP.Net,C#,MVC,我得到ASP.Net MVC無效的登錄嘗試

無效的登錄嘗試。

當我嘗試登錄。我註釋掉了電子郵件引用,因爲我只想使用用戶名和密碼登錄。在調試時,我驗證這些值是否正確,並且我還檢查了表中的值,以確保沒有任何使用戶名或密碼更長並且檢出的隱藏字符。

這裏是我的代碼:

AccountViewModels.cs:

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

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

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

     [Display(Name = "Remember me?")] 
     public bool RememberMe { get; set; } 
    } 

Login.cshtml:

<div class="row"> 
    <div class="col-md-8"> 
     <section id="loginForm"> 
      @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" })) 
      { 
       @Html.AntiForgeryToken() 
       <h4>Use a local account to log in.</h4> 
       <hr /> 
       @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
       @*<div class="form-group"> 
        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" }) 
        <div class="col-md-10"> 
         @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
         @Html.ValidationMessageFor(m => m.Email, "", new { @class = "text-danger" }) 
        </div> 
       </div>*@ 
       <div class="form-group"> 
       @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" }) 
       <div class="col-md-10"> 
        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" }) 
        @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" }) 
       </div> 
       </div> 
       <div class="form-group"> 
        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" }) 
        <div class="col-md-10"> 
         @Html.PasswordFor(m => m.Password, new { @class = "form-control" }) 
         @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" }) 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-md-offset-2 col-md-10"> 
         <div class="checkbox"> 
          @Html.CheckBoxFor(m => m.RememberMe) 
          @Html.LabelFor(m => m.RememberMe) 
         </div> 
        </div> 
       </div> 
       <div class="form-group"> 
        <div class="col-md-offset-2 col-md-10"> 
         <input type="submit" value="Log in" class="btn btn-default" /> 
        </div> 
       </div> 
       <p> 
        @Html.ActionLink("Register as a new user", "Register") 
       </p> 
       @* Enable this once you have account confirmation enabled for password reset functionality 
        <p> 
         @Html.ActionLink("Forgot your password?", "ForgotPassword") 
        </p>*@ 
      } 
     </section> 
    </div> 
    <div class="col-md-4"> 
     <section id="socialLoginForm"> 
      @Html.Partial("_ExternalLoginsListPartial", new ExternalLoginListViewModel { ReturnUrl = ViewBag.ReturnUrl }) 
     </section> 
    </div> 
</div> 

AccountController.cs:

// POST: /Account/Login 
     [HttpPost] 
     [AllowAnonymous] 
     [ValidateAntiForgeryToken] 
     public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) 
     { 
      if (!ModelState.IsValid) 
      { 
       return View(model); 
      } 

      // This doesn't count login failures towards account lockout 
      // To enable password failures to trigger account lockout, change to shouldLockout: true 
      var result = await SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout: false); 
      switch (result) 
      { 
       case SignInStatus.Success: 
        return RedirectToLocal(returnUrl); 
       case SignInStatus.LockedOut: 
        return View("Lockout"); 
       case SignInStatus.RequiresVerification: 
        return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }); 
       case SignInStatus.Failure: 
       default: 
        ModelState.AddModelError("", "Invalid login attempt."); 
        return View(model); 
      } 
     } 

我幾乎使用模板供應d由VS,並根據我在互聯網上找到的東西改變了一切,所以我不明白什麼是錯的。似乎它應該工作......

+0

你打開並激活ssl?一個很好的方式來說明,如果您的網站在默認地址的起始處有https:// – Abob

+0

「AccountController」中'result'的值是什麼?奇怪的是,你是否完全確信憑證*確實是正確的,而不是邏輯錯誤?也許嘗試創建另一個本地帳戶並登錄,因爲這是完全可以確定的? –

+0

在你的答案幫助下,我發現了這個問題。在編寫代碼和測試以及由於註冊時的幾種情況而失敗時,我有一些用戶名不在SQL表中。這些值顯然保存在某個地方,我需要以某種方式刪除它們,所以沒有再發生這種情況。爲了正確回答這個問題,任何人都可以提供解決方案來擺脫這些流氓用戶名? –

回答

0

model.UserName可以比較兩個字段:Email或UserName。 要定義使用哪個字段去App_Start \ IdentityConfig.cs

我認爲RequireUniqueEmail是真實的(使用電子郵件)。將其更改爲false(用戶名)可以解決問題。

public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) 
    { 
     var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 
     manager.UserValidator = new UserValidator<ApplicationUser>(manager) 
     { 
      RequireUniqueEmail = false 
     }; 
+0

感謝您的意見。 Ruard,在IdentityConfig.cs中,我將equireUniqueEmail更改爲false,但它不起作用。 Abob78,我在我的本地機器上運行它,並沒有發佈它與當前的變化。 Geoff James,就行了:var result = await SignInManager.PasswordSignInAsync(model.UserName,model.Password,model.RememberMe,shouldLockout:false);結果等於「Failure」。 model.UserName和model。密碼都是正確的。 –

0

由於您沒有指定註冊帳戶的方式,請查看模板是如何工作的。
如果您從網站註冊,那麼您的電子郵件也將成爲您的用戶名,因此當您登錄網站並輸入您的電子郵件時,它將成功,因爲您必須輸入的電子郵件與用戶名相同。
但是,如果您沒有在網站上註冊並指定用戶名與電子郵件不同,那麼您必須輸入您的用戶名。你可以從數據庫查看你的用戶名。