1

私はMVC4でWebアプリケーションを開発しており、ログインビューにはオプションがあります-この機能を実装するには、作成したCookieを使用しています-

WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)

ログイン方法は以下の通り

   [AllowAnonymous]
   public ActionResult Login()
   {       //Get persistant cookie
     HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; 
     if (!ReferenceEquals(authCookie, null))
     {      //Decrypt
         FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value); 
          string userName = Server.HtmlEncode(ticket.Name); //GetuserName
          // get Role and redirect in  as done after authentication
               return RedirectToDefaultView(returnUrl, userName); 
      }
            return View();
}

まあ、私はいくつかの懸念があります-これがベストプラクティスである場合、Cookieを確認しているだけで、クライアントマシンに存在する場合は、WebSecurity.Login(model.UserName、modelの後に呼び出されるRedirectToDefaultView(returnUrl、userName)メソッドを呼び出しています.Password、persistCookie: model.RememberMe) 以下のメソッドで..

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{ 
    if(WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
          return RedirectToDefaultView(returnUrl, userName); 
}

なんらかの理由でログイン コントロールでパスワードを保持できなかった後に選択したアプローチ、およびサイトで読むこともお勧めできません。

@Html.PasswordFor(model => model.Password, new { @class = "password-icon", @maxlength = 30 })

これに関する提案やヘルプは、gr8 の助けになります。

ありがとうHSR。

4

0 に答える 0