Hello All,
I am trying to redirect to the requested page after authentication. But after clicking login, the returnUrl is empty even though there is URL specified.
Below is my code
@using (Html.BeginForm("SignIn", "Login", new { ReturnUrl = Request.QueryString["ReturnUrl"]}, FormMethod.Post, new { @class = "login-form form-horizontal flex-column flex-center"})) {
<div class="flex-column flex-center flex-grow">
<img src="~/images/test.png" />
<!---->
@if (TempData["Msg"] != null) {
<div class="text-danger">
@TempData["Msg"].ToString()
</div>
}
<div class="margin-md">
<div class="ember-view input-group">
<span class="input-group-addon login-form-addon"><i class="fa fa-user"></i></span>
<input id="ember2302" class="ember-view ember-text-field form-control" autofocus=""
placeholder="Email address" name="UserEmail" type="text">
</div>
</div>
<div class="margin-md">
<div class="ember-view input-group">
<span class="input-group-addon login-form-addon"><i class="fa fa-lock"></i></span>
<input class="ember-view ember-text-field form-control" placeholder="Password" name="UserPassword"
type="password">
</div>
</div>
<a class="ember-view btn btn-link" href="ForgotPassword">Forgot your password?</a>
<div class="login-form__state-message login-form__state-message--success">
</div>
</div>
<div class="flex flex-split flex-align-stretch pad-lg background-grey">
<div>
</div>
<button class="btn btn-primary" type="submit" formaction="Login">
Sign In</button>
</div>
}
Controller
[HttpPost]
public ActionResult Login(tbl_UsersMaster UserInfo, string ReturnUrl)
{
ModelState.Remove("ConfirmPassword");
if (ModelState.IsValid)
{
try
{
if (Membership.ValidateUser(UserInfo.UserEmail, UserInfo.UserPassword))
{
Session["UserId"] = db.tbl_UsersMaster.Where(x => x.UserEmail == UserInfo.UserEmail && x.UserPassword == UserInfo.UserPassword).Select(x => x.UserId).SingleOrDefault();
//Session["UserId"] = objdb.GetAll().Where(x=>x.UserEmail==UserInfo.UserEmail && x.UserPassword==UserInfo.UserPassword);
FormsAuthentication.SetAuthCookie(UserInfo.UserEmail, false);
//FormsAuthentication.SetAuthCookie(UserInfo.UserId, false);
int UserRole = int.Parse(db.tbl_UsersMaster.Where(x => x.UserEmail == UserInfo.UserEmail && x.UserPassword == UserInfo.UserPassword).Select(x => x.UserRole).SingleOrDefault().ToString());
if (ReturnUrl != null)
{
return RedirectToAction(ReturnUrl);
}
if (UserRole == 2 || UserRole == 3)
{
return RedirectToAction("History", "Design", new { area = "Designers" });
}
else
{
return RedirectToAction("Index", "Actions", new { area = "Managers" });
}
}
else
{
TempData["Msg"] = "Login Failed. Incorrect Email or Password";
return RedirectToAction("Login");
}
}
catch (Exception ex)
{
TempData["Msg"] = "Login Failed." + ex.Message;
return RedirectToAction("Login");
}
}
else
{
TempData["Msg"] = "Please enter UserName & Password";
return RedirectToAction("Login");
}
}