MVC5身份验证无法保存UseCookieAuthentication

时间:2015-09-10 04:37:21

标签: authentication asp.net-mvc-5

我遇到MVC 5身份验证问题 成功登录后,我想我无法保存UseCookieAuthentication。

  • 我的启动代码:
    public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
         app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath = new PathString("/MyAccount/Login")
                });
                // Use a cookie to temporarily store information about a user logging in with a third party login provider
                app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    }
}
  • 我的登录控制器:

    public ActionResult Login(Login l, string ReturnUrl="")
    {
        using (CBDB2012Entities dc = new CBDB2012Entities())
        {
            var user = dc.Users.Where(a => a.UserName.Equals(l.Username) && a.Password.Equals(l.Password)).FirstOrDefault();
            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(user.UserName, l.RememberMe);
                if (Url.IsLocalUrl(ReturnUrl))
                {
                    return Redirect(ReturnUrl);
                }
                else
                {
                    return RedirectToAction("MyProfile", "MyAccount");
                }
            }
        }
        ModelState.Remove("Password");
        return View();
    }
    

登录后,它是RedirectToAction(" MyProfile"," MyAccount"); 但在MyProfile视图中,我看不到任何关于用户的信息,我无法通过[授权]访问contac页面:

- 联系控制器

    [Authorize]
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }

1 个答案:

答案 0 :(得分:0)

我在登录控制中通过会话测试,它运行正常:

var user = dc.Users.Where(a => a.UserName.Equals(l.Username) && a.Password.Equals(l.Password)).FirstOrDefault();
            if (user != null)
            {
                FormsAuthentication.SetAuthCookie(user.UserName, l.RememberMe);
                Session["loginname"] = user.Employee.FirstName;

                if (Url.IsLocalUrl(ReturnUrl))
                {
                    return Redirect(ReturnUrl);
                }
                else
                {
                    return RedirectToAction("MyProfile", "MyAccount");
                }
            }

我可以从数据库中获取Session [“loginname”]。