ASP.NET MVC记住我 [英] ASP.NET MVC Remember me

查看:1215
本文介绍了ASP.NET MVC记住我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了总部设在ASP.NET MVC项目4那么简单的身份验证。

我试图让我的网站自动登录,当他们检查记得我复选框用户。不过我在得到这个工作的问题。关闭了浏览器并重新打开它的用户后,从来没有记录下来。

检查后( http://forums.asp.net/t/1654606.aspx #4310292 )我已在一台机器的关键,IIS生成。我给自己定的在运行时的自动生成的生成每个应用程序的唯一键的双双被禁用,我已经生成密钥)。不幸的是这并没有奏效。

看着<一个href=\"http://stackoverflow.com/questions/513949/remember-me-with-asp-net-mvc-authentication-is-not-working\">"Remember ME&QUOT;在ASP.NET MVC认证不工作,我在该行的 FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe)已经添加了的,但没有工作,要么所以我现在已经评论了出来。

我试过 ASP.NET MVC了rememberMe 给出的答案但这似乎并没有要么工作。

我失去了一些东西明显?

  // FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe);如果(model.RememberMe)
{
    // INT超时= model.RememberMe? 525600:2; //超时分钟,525600 =365天
    INT超时= 525600;
    VAR票=新的FormsAuthenticationTicket(model.UserName,model.RememberMe,超时);
    字符串加密= FormsAuthentication.Encrypt(票);
    变种的Cookie =新的HttpCookie(FormsAuthentication.FormsCookieName,加密);
    cookie.Expires = System.DateTime.Now.AddMinutes(超时); //我行
    Response.Cookies.Add(饼干);
}


解决方案

这是我要做的事。

 公共类MyAuthentication
{
    公共静态的HttpCookie GetAuthenticationCookie(LoginModel模型,布尔persistLogin)
    {
         //用户数据存储在ticktet然后cookie数据
        JS的JavaScriptSerializer =新的JavaScriptSerializer();        VAR用户数据= js.Serialize(模型);
        的FormsAuthenticationTicket authTicket =新的FormsAuthenticationTicket(
                 1,
                 阿卡什
                 DateTime.Now,
                 DateTime.Now.AddHours(1),
                 persistLogin,
                 用户数据);        字符串encTicket = FormsAuthentication.Encrypt(authTicket);
        的HttpCookie饼干=新的HttpCookie(FormsAuthentication.FormsCookieName,encTicket);
        cookie.Expires = authTicket.Expiration; //必须为Cookie过期做
        返回的cookie;
    }    内部静态布尔登录(用户名字符串,字符串密码)
    {
        //用户名=阿卡什密码=阿卡什
        //检查可以通过数据库来完成
        如果(用户名==阿卡什&放大器;&放大器;密码==阿卡什)
            返回true;
        其他
            返回false;
    }
}

然后

  [HTTPGET]
    [使用AllowAnonymous]
    公众的ActionResult登录()
    {
        //ViewBag.Message =您的联系页面。
        的HttpCookie饼干= Request.Cookies时[FormsAuthentication.FormsCookieName]
       // VAR EK = cookie.Value;
        尝试
        {
            在浏览器//有时无的cookie
            JS的JavaScriptSerializer =新的JavaScriptSerializer();
            的FormsAuthenticationTicket票= FormsAuthentication.Decrypt(cookie.Value);
            //字符串数据= ticket.UserData;
            LoginModel模型= js.Deserialize&LT; LoginModel&GT;(ticket.UserData);
            如果(MyAuthentication.Login(model.UserName,model.Password)==真)
            {
                RedirectToAction(索引,家);
            }
        }
        抓住
        {        }
        返回查看();

您可以检查它的Global.asax或授权过滤器。
请确保您有web.config中有

 &LT;身份验证模式=表格&GT;
  &LT;形式defaultUrl =/首页/登录loginUrl =/家/登录超时=2880&GT;
  &LT; /形式GT;
&LT; /认证&GT;

和[授权]所有控制器之前的属性。

I've got a project based in ASP.NET MVC 4 that simple authentication.

I'm trying to get my site to automatically log the user in when they check the remember me checkbox. However I'm having problems getting this working. After closing down the browser and reopening it the user is never logged in.

After checking (http://forums.asp.net/t/1654606.aspx#4310292) I've added a machine key in, generated by IIS. I've set automatically generate at runtime and generate a unique key for each application have both been disabled and I've Generated Keys). Unfortunately this hasn't worked.

Looking at "Remember me" with ASP.NET MVC Authentication is not working, I've added in the line FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe) but that didn't work either so I've now commented it out.

I tried the answer given on ASP.NET MVC RememberMe but that doesn't seem to work either.

Am I missing something obvious?

//FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);

if (model.RememberMe)
{
    //int timeout = model.RememberMe ? 525600 : 2; // Timeout in minutes,525600 = 365 days
    int timeout = 525600;
    var ticket = new FormsAuthenticationTicket(model.UserName, model.RememberMe, timeout);
    string encrypted = FormsAuthentication.Encrypt(ticket);
    var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
    cookie.Expires = System.DateTime.Now.AddMinutes(timeout);//My Line
    Response.Cookies.Add(cookie);
}

解决方案

this is how i do it

public class MyAuthentication
{
    public static HttpCookie GetAuthenticationCookie(LoginModel model, bool persistLogin)
    {
         // userData storing data in ticktet and then cookie 
        JavaScriptSerializer js = new JavaScriptSerializer();

        var userData = js.Serialize(model);
        FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                 1,
                 "akash",
                 DateTime.Now,
                 DateTime.Now.AddHours(1),
                 persistLogin,
                 userData);

        string encTicket = FormsAuthentication.Encrypt(authTicket);
        HttpCookie cookie= new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
        cookie.Expires = authTicket.Expiration; //must do it for cookie expiration 
        return cookie;
    }

    internal static bool Login(string UserName, string Password)
    {
        //UserName="akash" Password="akash"
        //check can be done by DB
        if (UserName== "akash" && Password == "akash")
            return true;
        else
            return false;
    }
}

and then

[HttpGet]
    [AllowAnonymous]
    public ActionResult Login()
    {
        //ViewBag.Message = "Your contact page.";
        HttpCookie cookie =  Request.Cookies[FormsAuthentication.FormsCookieName];
       // var ek = cookie.Value;
        try
        {
            //some times no cookie in browser
            JavaScriptSerializer js = new JavaScriptSerializer();
            FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);
            //string data = ticket.UserData;
            LoginModel model = js.Deserialize<LoginModel>(ticket.UserData);
            if (MyAuthentication.Login(model.UserName, model.Password) == true)
            {
                RedirectToAction("Index", "Home");
            }
        }
        catch
        {

        }
        return View();

you can check it on Global.asax or authorization filter. make sure you have web.config has

<authentication mode="Forms">
  <forms defaultUrl="/Home/Login" loginUrl="/home/Login" timeout="2880">
  </forms>
</authentication>

and [Authorize] attribute before all controller.

这篇关于ASP.NET MVC记住我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆