2015-09-10 54 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」]。