i have the model like this
public class LoginModel
{
[Required]
[Display(Name = "User name")]
[EmailAddress(ErrorMessage = "Invalid Email Address")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
public string ReturnUrl { get; set; }
}
i have the controller like this
using MusicStore.Models;
using System;
using MusicStore.AppCode;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace MusicStore.Areas.Admin.Controllers
{
public class LoginController : Controller
{
[AllowAnonymous]
public ActionResult Index(string returnUrl)
{
LoginModel objLoginModel = new LoginModel();
objLoginModel.ReturnUrl = returnUrl;
return View(objLoginModel);
}
[HttpPost]
public ActionResult Index(LoginModel model)
{
DataContext odata =new DataContext();
LoginInfo ologininfo = odata.Login(model.UserName,model.Password);
Session["UserDetails"] = ologininfo;
Session["Name"] = ologininfo.FirstName;
if (ologininfo.RoleID != null)
{
FormsAuthentication.SetAuthCookie("AuthenticationCookie",model.RememberMe);
if (model.ReturnUrl != null )
{
return Redirect(model.ReturnUrl);
}
else
{
if (ologininfo.RoleID == 1)
{
return RedirectToAction("Index", "Roles");
}
else if(ologininfo.RoleID == 2)
{
return RedirectToAction("Index", "Home/AddPatient", new { @area = "" });
}
else if(ologininfo.RoleID == 3)
{
return RedirectToAction("Index", "Doctor/Index", new { @area = "" });
}
else if (ologininfo.RoleID == 4)
{
return RedirectToAction("Index", "Test");
}
else
{
return RedirectToAction("Index", "Home", new { @area = ""});
}
}
}
else
{
ModelState.AddModelError("admin/patient/patient", "The user name or password is incorrect.");
return View(model);
}
}
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Clear();
return RedirectToAction("Index","Login");
}
}
}
i have the view like this
<legend>Please Login</legend>
<form action='/Admin/Login' method='POST' id='loginForm' class='form-signin' autocomplete='off'>
<div class="form-inner">
@Html.ValidationSummary()
<div class="input-prepend">
class="icon-envelope">
@Html.TextBoxFor(m => m.UserName,new{@class="span4"})
</div>
<div class="input-prepend">
^__i class="icon-key">
@Html.PasswordFor(m => m.Password,new{@class="span4"})
</div>
<div>
<label class="checkbox" for='remember_me'>
Remember me
<big><big><small><small></small></small></big></big> @Html.CheckBoxFor(m=>m.RememberMe)
</label>
<a href="">
Forgot Password
</a>
<a href="">
Change Password
</a>
</div>
@Html.HiddenFor(m =>m.ReturnUrl)
</div>
<footer class="signin-actions">
<input class="btn btn-primary" type='submit' id="submit" value='Login'/>
</footer>
</form>
<pre>