3

In my C# MVC4 application, I have two tabs at the top of every view. One labeled edit and the other labeled Admin. When either of these two tabs are clicked the user is redirected to a login page. In the Login ActionResult, after the user has been validated, I perform a check to see if the user belongs to specific AD groups that I use as roles. This all works correctly. My issue is trying to detect and perform actions based upon whether the user accessed login by clicking Edit or by clicking Admin.

How can I grab this information? Ive tried something like:

if(HttpContext.Request.UrlReferrer.OriginalString.Contains("Edit"))

but the Url that contains is something like Account/Login.

My current code is:

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);
        }

The two tabs are created in my layout.cshtml like this:

 <ul id="menu">
                        <li>@Html.ActionLink("Edit", "Login", "Account")</li>
                        <li>@Html.ActionLink("Admin", "Admin", "Home")</li>
                    </ul>
4

1 回答 1

1

我看到您没有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)

你是这个意思吗?

于 2013-04-21T22:22:14.393 回答