Tuesday, 13 August 2013

Login Popup

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;


namespace ABC.Controllers
{

    [Authorize]
    public class AccountController : Controller
    {
        public static readonly string USER = "User";

        DataContext context = new DataContext();
        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login()
        {
            return ContextDependentView();
        }

        //
        // POST: /Account/JsonLogin

        [AllowAnonymous]
        [HttpPost]
        public JsonResult JsonLogin(LoginDTO model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
             
                int st = context.Users.Where(p => p.Emai == model.Email && p.password == model.Password).Count();
                if (st == 1)
                {
                    var udet = context.Users.Where(p => p.Emai == model.Email && p.password == model.Password).SingleOrDefault();

                    if (udet != null)
                    {
                        int utid = udet.UtID;
                        Registration dto = new Registration();
                        dto.UID = udet.UID;
                        dto.UtID = udet.UtID;
                        dto.Email = udet.Emai;
                        dto.Firstname = udet.Firstname;

                        HttpContext.Session[USER] = dto;


                        if (utid == 1)
                        {
                            HttpContext.User.IsInRole("Admin");
                            return Json(new { success = true, redirect = Url.Action("Index", "Admin", new { area = "Admin" }) });
                        }
                        else if (utid == 2)
                        {
                            HttpContext.User.IsInRole("User");
                            return Json(new { success = true, redirect = Url.Action("Index", "User", new { area = "User" }) });
                        }
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Invalid Username/Password.");
                }
            }
            // If we got this far, something failed
            return Json(new { errors = GetErrorsFromModelState() });
        }
   
        //
        // GET: /Account/LogOff

        public ActionResult LogOff()
        {          
            return RedirectToAction("Index", "Home");
        }
   
        private ActionResult ContextDependentView()
        {
            string actionName = ControllerContext.RouteData.GetRequiredString("action");
            if (Request.QueryString["content"] != null)
            {
                ViewBag.FormAction = "Json" + actionName;
                return PartialView();
            }
            else
            {
                ViewBag.FormAction = actionName;
                return View();
            }
        }

        private IEnumerable<string> GetErrorsFromModelState()
        {
            string[] str = new string[] { "Invalid Username/Password."};  
            return str;
        }    

    }
}

No comments:

Post a Comment