![](/img/trans.png)
[英]How to add two different login url in form based authentication in C# MVC4?
[英]C# MVC4 Login Actions Based on Route Taken to Login
在我的C#MVC4應用程序中,每個視圖的頂部都有兩個選項卡。 一個標記為編輯,另一個標記為Admin。 單擊這兩個選項卡中的任何一個時,用戶將被重定向到登錄頁面。 在Login ActionResult中,在驗證用戶之后,我會檢查用戶是否屬於我用作角色的特定AD組。 一切正常。 我的問題是嘗試根據用戶是通過單擊編輯還是單擊管理員來訪問登錄來檢測和執行操作。
我怎樣才能抓住這些信息? 我嘗試過類似的東西:
if(HttpContext.Request.UrlReferrer.OriginalString.Contains("Edit"))
但包含的網址類似於Account/Login
。
我目前的代碼是:
public ActionResult Login(LoginModel model, string returnUrl)
{
//Checks if user exists in Active Directory
if (ModelState.IsValid && Membership.ValidateUser(model.UserName, model.Password))
{
//Logs user in by creating a cookie
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
//Checks to see if user belongs to a group with Edit or Admin Priviledges
if ((Roles.IsUserInRole(model.UserName,"TCL-CAdmin")) || (Roles.IsUserInRole(model.UserName, "TCL-CGroup")))
{
return RedirectToAction("Index", "Home");
}
else
{
return RedirectToAction("Index_Perm", "Home");
}
}
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
這兩個標簽是在我的layout.cshtml中創建的,如下所示:
<ul id="menu">
<li>@Html.ActionLink("Edit", "Login", "Account")</li>
<li>@Html.ActionLink("Admin", "Admin", "Home")</li>
</ul>
我看到你沒有在你的控制器動作中使用returnUrl
參數,你可以像這樣傳遞它:
@Url.Action("Login", "Account", new { returnUrl = Request.RawUrl })
除此之外,您還可以傳遞另一個參數作為上下文,例如:
@Url.Action("Login", "Account", new { returnUrl = Request.RawUrl, context = "Edit" })
並將您的操作簽名更改為:
public ActionResult Login(LoginModel model, string returnUrl, string context)
你是這個意思嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.