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>
user979331
  • 11,039
  • 73
  • 223
  • 418

1 Answers1

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>
Krishnraj Rana
  • 6,516
  • 2
  • 29
  • 36