我似乎無法弄清楚如何在認證用戶後返回上一頁。我試着返回RedirectToLocal(returnUrl),但總是把用戶帶回到主頁索引頁面。當我使用return Redirect(returnUrl)時,它會重新載入前一頁。但是,前一頁的ViewBag中的對象在該點處爲空。我只想讓用戶返回到上一頁,保留狀態,將所有輸入的數據保留在購物車結賬頁面上。在認證用戶後重定向用戶返回上一頁
1
A
回答
0
你如何將他們重定向到登錄頁面 - 手動或使用屬性?將[Authorize]
添加到控制器類是最簡單的方法,默認情況下按預期工作。例如
[Authorize] public class TestController : Controller
對於你的情況,你可能要放置在授權上的操作屬性,而不是整個控制器
[Authorize]
public ActionResult ConfirmShoppingCart()
這種方法的好處是進一步customization
一個可能的最後手段是手動放置一個條件的視圖呈現您的「繼續」按鈕(或任何你想觸發認證)。
E.g.
@if (Request.IsAuthenticated) {
//normal proceed'
} else {
@Html.ActionLink("Proceed", "Login", "Account", new { @returnUrl = Url.Action("ViewCart", "ShoppingCart")}, new { })
}
0
使用後::
[HttpPost]
public ActionResult Login(User model, string returnUrl)
{
// Lets first check if the Model is valid or not
if (ModelState.IsValid)
{
using (DbEntities entities = new DbEntities())
{
string username = model.UserID;
string password = model.Password;
var UserAuth = db.Users.Where(x => x.UserID == model.UserID && x.Password == model.Password).FirstOrDefault();
// Now if our password was enctypted or hashed we would have done the
// same operation on the user entered password here, But for now
// since the password is in plain text lets just authenticate directly
bool userValid = entities.Users.Any(user => user.UserID == username && user.Password == password);
var usr = entities.Users.FirstOrDefault(x => x.UserID == username && x.Password == password);
if (userValid)
{
//System.Web.HttpContext.Current.Session["_SessionCompany"] = usr.DefaultCompanyID;
FormsAuthentication.SetAuthCookie(username, false);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
//return RedirectToAction("Index", "Dossier");
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
+0
當我使用重定向(returnUrl)時,它「重新加載」前一頁,用戶輸入的所有數據都是空白的。我想要做一些事情,比如在javascript中回顧歷史。那可能嗎? – user2974739
+0
使用斷點來檢查什麼是'returnUrl'值 – Rahul
相關問題
- 1. 用戶認證後重定向回頁面標籤?
- 2. symfony認證後重定向用戶
- 3. Django:重定向用戶兩頁返回
- 4. 設計:認證用戶後重定向到上一頁? (request.referer結果循環)
- 5. 如何在認證後重定向用戶
- 6. 經過Linkedin認證。將用戶重定向迴應用程序
- 7. 重定向後驗證用戶
- 8. cakephp - 在「Missing method」錯誤上重定向未認證的用戶
- 9. CakePHP:如果用戶被認證,將用戶從/重定向到
- 10. Xamarin.Auth PageRenderer在用戶認證之後重定向到PCL中的頁面
- 11. 用戶登錄後頁面重定向
- 12. Zend認證和登錄後重定向用戶
- 13. 登錄後將用戶重定向到上一頁
- 14. 將返回的用戶重定向到特定頁面
- 15. 如何在用戶登錄後重定向回用戶期望訪問頁面?
- 16. 將用戶重定向到前一頁
- 17. PHP重定向用戶返回登錄頁面
- 18. 用戶確認帳戶後覆蓋重定向路徑 - 設計
- 19. 在IdP認證/ SP重定向之前向用戶帳戶追加文本
- 20. 在用戶確認時使用jquery重定向到新頁面
- 21. 用戶單擊取消後重定向回網頁
- 22. Spring Security:SecurityContextHolder.getContext()。在用戶註冊用戶並重定向到登錄頁面後,getAuthentication()返回NULL
- 23. FB.login認證後重定向
- 24. 如何將用戶重定向到一個返回的URL python
- 25. 將用戶重定向到我的分頁上的最後一頁
- 26. 如何在沒有重定向的OAuthCallBack後返回客戶端
- 27. ZK - 將用戶重定向回前一頁
- 28. SpecialFolder.ApplicationData返回默認用戶
- 29. 基於原始認證在會話超時後強制重定向用戶
- 30. 登錄後轉發用戶返回上一頁
我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –