ASP.NET MVC Active Directory身份验证返回URL问题

时间:2018-01-29 17:05:18

标签: c# asp.net asp.net-mvc

我正在使用ASP.NET MVC进行Active Directory身份验证并且运行良好,我唯一的问题是登录方法上的返回URL:

[HttpPost]
        public ActionResult Login(LoginClass model, string ReturnUrl)
        {

            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                    if (Url.IsLocalUrl(ReturnUrl) && ReturnUrl.Length > 1 && ReturnUrl.StartsWith("/")
                        && !ReturnUrl.StartsWith("//") && !ReturnUrl.StartsWith("/\\"))
                    {
                        return Redirect(ReturnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "The user name or password provided is incorrect");
                }
            }

            return RedirectToAction("Index", "Home");
        }

我遇到的问题是,当URL看起来像这样时,我的ReturnUrl是alwasy null:

Login/Index?ReturnUrl=%2fConsultant%2fIndex

为什么我的ReturnUrl在填充参数时总是为空?

以下是我的观点:

<div class="container">
    <form action="~/Login/Login" id="Login" method="post">
        <div class="row">
            <div class="col-md-12">
                <p>
                    <label for="username">Username</label>
                    <input type="text" id="username" name="username" class="form-control" />
                </p>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <p>
                    <label for="password">Password</label>
                    <input type="password" id="password" name="password" class="form-control" />
                </p>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <p>
                    <input type="submit" id="submit" name="submit" value="Login" class="btn btn-default" />
                </p>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <p>
                    <label for="password">Remember Me?</label>
                    <input type="checkbox" id="chkPersist" name="chkPersist" />
                </p>
            </div>
        </div>
    </form>
</div>

以下是web.config中的一些代码:

<authentication mode="Forms">
      <forms name=".ADAuthCookie" loginUrl="~/Login/Index" timeout="45" slidingExpiration="false" protection="All" />
    </authentication>
    <membership defaultProvider="ADMembershipProvider">
      <providers>
        <clear />
        <add name="ADMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider" connectionStringName="ADConnectionString" attributeMapUsername="sAMAccountName" />
      </providers>
    </membership>

1 个答案:

答案 0 :(得分:1)

您需要在表单标记中提及ReturnUrl

试试这个 -

<div class="container">
 @using (Html.BeginForm("Login", "Login", new { ReturnUrl = Request.QueryString["ReturnUrl"] }, FormMethod.Post, new { @id = "Login" }))   
 {
        <div class="row">
            <div class="col-md-12">
                <p>
                    <label for="username">Username</label>
                    <input type="text" id="username" name="username" class="form-control" />
                </p>
            </div>
        </div>
        ... blah... blah....
  }
</div>