在我的C# MVC4应用程序中,每个视图顶部都有两个标签页,一个标注为“编辑”,另一个标注为“管理员”。当单击这两个标签之一时,用户将重定向到登录页面。在Login ActionResult中,在验证了用户之后,我进行了检查,以查看用户是否属于我用作角色的特定AD组。这一切都正常工作。我的问题是如何检测并执行基于用户是通过单击编辑还是单击管理员来访问登录而采取行动的信息。
我该如何获取此信息? 我尝试了类似以下代码:
我的layout.cshtml文件中创建这两个标签页的代码如下所示:
我该如何获取此信息? 我尝试了类似以下代码:
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>
Url.Action
方法仅返回 URL,而不是a
标签。因此,您需要使用<a href="@Url.Action(...)">Edit</a>
等。 - greg84http://localhost:1234/
或类似网址 - 那么就是正确的。返回URL是根路径。 - greg84