基于登录路由的 C# MVC4 登录操作

标签 c# url asp.net-mvc-4 authentication authorization

在我的 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/

相关文章:

c# - ASP.NET Paypal IPN 返回 VERIFIED 但 IPN 发送失败

url - 如何以 REST 风格获取只读资源与可编辑资源?

JavaScript 警报没有响应

c# - 嵌套 MySQL 查询的解决方法?

C# 反射 : how to get an array values & length?

c# - YamlDotNet 将整数反序列化为数字而不是字符串

url - angularjs $location.hash 使##,但我需要#

javascript - 使用 JS 获取网站 URL 不适用于 https

c# - 从 asp.net mvc 4 razor 中的自定义 Html Helpers 访问属性

asp.net-mvc-4 - 从 beta 升级后 Autofac/MVC4/WebApi (RC) 依赖注入(inject)问题