2016-06-14 25 views
0

私はASP.NET MVC5アプリケーションを開発中で、RememberMe機能を実装したいと思います。ここで、ユーザがRememberMeをチェックすると、ユーザが再度ウェブサイトに行くたびに、ユーザ名がユーザ名フィールドに自動的にフェッチされるはずです。私は以下の方法で試した。しかし、それには問題があり、私はこれを行うことができません。誰も私にこれを手伝ってもらえますか?Remember Me - ユーザ名を取得する - MVC

var authTicket = new FormsAuthenticationTicket(
              1, 
              userId, //user id 
              DateTime.Now, 
              DateTime.Now.AddMinutes(50), // expiry 
              model.RememberMe, //true to remember 
              string.Empty 
             ); 

HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(authTicket)); 
if (authTicket.IsPersistent) 
{ 
    cookie.Expires = authTicket.Expiration; 
} 
System.Web.HttpContext.Current.Response.Cookies.Add(cookie); 

ありがとうございます。

答えて

0

以下のコードを使用してこれを行いました。コントローラーで :ログイン初期アクションで

if(model.RememberMe == true) 
        { 
         Response.Cookies["email"].Value = model.Email; 
         Response.Cookies["email"].Expires = DateTime.Now.AddDays(365); 
        } 

:ビューで

if (Request.Cookies["email"] != null) 
       ViewBag.email = Request.Cookies["email"].Value; 

@if (ViewBag.email != null) 
    { 
    @Html.TextBoxFor(m => m.Email, new { @class = "form-control", @Value = ViewBag.email }) 
    } 
    else 
    { 
    @Html.TextBoxFor(m => m.Email, new { @class = "form-control" }) 
    } 
ログインPOSTアクションで
関連する問題