2014-07-17 70 views
0

感谢大家阅读我的话题。但我需要你的帮助! 我有一个Asp.NET MVC Action的问题。需要登录才能做出动作

在主页。我有一个链接重定向到一个动作调用checkTicket(),但需要登录。

所以,在checkTicket()方法中。我正在使用以下代码来检查批准:

if (Request.IsAuthenticated) 
{ 
    return View(); 
} 
else 
{ 
    return RedirectToAction("Login", "Account"); 
} 

但是在操作中登录帐户控制器。我怎样才能返回checkTicket的View()?

这是我想要的东西。 主页(点击) - > checkTicket(要求) - >登录(返回) - > checkTicket()

回答

0

创建一个设置cookie,让你知道用户想要checkticket但没有登录:

if (Request.IsAuthenticated) 
{ 
    return View(); 
} 
    else 
{ 
    //The cookie's name is UserSettings 
    HttpCookie myCookie = new HttpCookie("UserSettings"); 

    //The subvalue of checkticket is = true 
    myCookie["checkticket"] = "true"; 

    //The cookie expires 1 day from now 
    myCookie.Expires = DateTime.Now.AddDays(1d); 

    //Add the cookie to the response 
    Response.Cookies.Add(myCookie); 

    return RedirectToAction("Login", "Account"); 
} 

然后在你的登录操作,检查是否存在像这样的饼干:

if (Request.Cookies["UserSettings"] != null) 
{ 
    string userSettings; 
    if (Request.Cookies["UserSettings"]["checkticket"] != null) 
    { 
     userSettings = Request.Cookies["UserSettings"]["checkticket"]; 
    } 

    if(userSettings) { 
     //redirect to checkticket 
    } else { 
     // redirect to your normal view 
    } 
} 

* MSDN的代码礼貌:write cookieread cookie

+0

感谢FO r你的帮助:) 美好的一天兄弟 – user2165201

+0

不客气@ user2165201 – Zac

相关问题