Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have the model like this
C#
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
    {
        //
        // GET: /Admin/Login/
        [AllowAnonymous]
        public ActionResult Index(string returnUrl)
        {
            LoginModel objLoginModel = new LoginModel();
            objLoginModel.ReturnUrl = returnUrl;
            return View(objLoginModel);
        }

        [HttpPost]
        public ActionResult Index(LoginModel model)
        {
            //stored procedure 
            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>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900