2015-09-10 55 views
0

我有MVC 5身份验证问题 成功登录后,我想我无法保存UseCookieAuthentication。MVC5身份验证无法保存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); 
    } 
} 
  • 我登录CONTROLER:

    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”); 但是在我的资料来看,我不能看到用户的内容和我上无法获得联系页面[授权]:

- 联系CONTROLER

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

     return View(); 
    } 

回答

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”]。