在我的 C# MVC4 应用程序中,每个 View 的顶部都有两个选项卡。一个标记为编辑,另一个标记为管理。单击这两个选项卡中的任何一个时,用户将被重定向到登录页面。在登录 ActionResult 中,验证用户后,我执行检查以查看用户是否属于我用作角色的特定 AD 组。这一切都正常工作。我的问题是尝试根据用户是通过单击“编辑”还是通过单击“管理”访问登录来检测和执行操作。
我怎样才能获取这些信息?我试过类似的东西:
if(HttpContext.Request.UrlReferrer.OriginalString.Contains("Edit"))
但包含的 Url 类似于 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>
最佳答案
我看到您没有在 Controller 操作中使用 returnUrl
参数,您可以像这样传递它:
@Url.Action("Login", "Account", new { returnUrl = Request.RawUrl })
除此之外,您还可以传递另一个参数作为上下文,例如:
@Url.Action("Login", "Account", new { returnUrl = Request.RawUrl, context = "Edit"})
并将您的操作签名更改为:
public ActionResult Login(LoginModel 模型,字符串 returnUrl,字符串上下文)
你是这个意思吗?
关于基于登录路由的 C# MVC4 登录操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16136759/