简体   繁体   English

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

[英]C# MVC4 Login Actions Based on Route Taken to Login

In my C# MVC4 application, I have two tabs at the top of every view. 在我的C#MVC4应用程序中,每个视图的顶部都有两个选项卡。 One labeled edit and the other labeled Admin. 一个标记为编辑,另一个标记为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. 在Login ActionResult中,在验证用户之后,我会检查用户是否属于我用作角色的特定AD组。 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 . 但包含的网址类似于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: 这两个标签是在我的layout.cshtml中创建的,如下所示:

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

I see you're not using the returnUrl parameter in your controller action, you can pass it like this: 我看到你没有在你的控制器动作中使用returnUrl参数,你可以像这样传递它:

@Url.Action("Login", "Account", new { returnUrl = Request.RawUrl })

In addition to this you could pass another parameter as the context, such as this: 除此之外,您还可以传递另一个参数作为上下文,例如:

@Url.Action("Login", "Account", new { returnUrl = Request.RawUrl, context = "Edit" })

And change the signature of your action to this: 并将您的操作签名更改为:

public ActionResult Login(LoginModel model, string returnUrl, string context)

Is that what you mean? 你是这个意思吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM