【问题标题】:MVC5 Authentication cannot save UseCookieAuthenticationMVC5 Authentication 无法保存 UseCookieAuthentication
【发布时间】:2015-12-06 05:33:18
【问题描述】:

我遇到了 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 视图中,我看不到有关用户的任何信息,也无法使用 [Authorize] 访问联系页面:

-联系人控制器

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

        return View();
    }

【问题讨论】:

    标签: authentication asp.net-mvc-5


    【解决方案1】:

    我在登录控制中通过会话进行了测试,它工作正常:

    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"] 视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-26
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-16
      • 2010-12-26
      相关资源
      最近更新 更多