0

I am using ASP.NET MVC for Active Directory Authentication and its working well, the only problem I have is the return URL on Login Method:

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

The problem I am experiencing is that my ReturnUrl is alwasy null when the URL looks like this:

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

Why is my ReturnUrl always null when the parameter is populated?

Here is my view:

<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>

Here is some code from 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>
5
  • Can you post the code of your view (.cshtml) file? Commented Jan 29, 2018 at 17:13
  • If it helps, query string values are parsed using the QueryStringValueProvider. It is possible (though unlikely) that another provider that is registered before QueryStringValueProvider is providing a null value before it has a chance to run. Commented Jan 29, 2018 at 17:14
  • Try to use RedirectToLocal(ReturnUrl); Commented Jan 29, 2018 at 17:17
  • @JCM ReturnUrl is null before it hits that point. Commented Jan 29, 2018 at 17:19
  • @KrishnrajRana I posted my view code. Commented Jan 29, 2018 at 17:23

1 Answer 1

1

You need to mention ReturnUrl in your form tag.

try this -

<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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.