public ActionResult Login(User userDetails)
 {
     ServiceLayer.Models.User user = _userService.GetUser(userDetails.EmailId, userDetails.Pwd);
     if (user != null)
     {
         CustomPrincipalSerializedModel serializeModel = new CustomPrincipalSerializedModel();
         serializeModel.Id        = user.Uid;
         serializeModel.FirstName = user.Fname;
         serializeModel.LastName  = user.Lname;
         serializeModel.Role      = user.UserRole.RoleName;
         JavaScriptSerializer serializer      = new JavaScriptSerializer();
         string userDataSerialized            = serializer.Serialize(serializeModel);
         FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
             1,
             user.EmailId,
             DateTime.Now,
             DateTime.Now.AddMinutes(30),
             false,
             userDataSerialized);
         string     encTicket = FormsAuthentication.Encrypt(authTicket);
         HttpCookie faCookie  = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);
         Response.Cookies.Add(faCookie);
         return(RedirectToAction("Home"));
     }
     @ViewBag.LoginStatus = false;
     return(View());
 }
コード例 #2
0
        protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
        {
            HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

            if (authCookie != null)
            {
                FormsAuthenticationTicket      authTicket     = FormsAuthentication.Decrypt(authCookie.Value);
                JavaScriptSerializer           serializer     = new JavaScriptSerializer();
                CustomPrincipalSerializedModel serializeModel = serializer.Deserialize <CustomPrincipalSerializedModel>(authTicket.UserData);
                CustomPrincipal newUser = new CustomPrincipal(authTicket.Name);
                newUser.Id               = serializeModel.Id;
                newUser.FirstName        = serializeModel.FirstName;
                newUser.LastName         = serializeModel.LastName;
                newUser.Role             = serializeModel.Role;
                HttpContext.Current.User = newUser;
            }
        }
コード例 #3
0
        private void CreateAuthenticationTicket(string username)
        {
            var authUser       = _userService.GetUser(username);
            var serializeModel = new CustomPrincipalSerializedModel
            {
                FirstName = authUser.FirstName,
                LastName  = authUser.LastName,
                Alias     = authUser.Alias,
                Avatar    = authUser.AvatarUrl
            };

            var serializer = new JavaScriptSerializer();
            var userData   = serializer.Serialize(serializeModel);

            var    authTicket = new FormsAuthenticationTicket(1, username, DateTime.Now, DateTime.Now.AddHours(8), false, userData);
            string encTicket  = FormsAuthentication.Encrypt(authTicket);
            var    faCookie   = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket);

            Response.Cookies.Add(faCookie);
        }
コード例 #4
0
        public ActionResult Login(string UserName, string Password, string returnURL)
        {
            Account acc = BUSAccount.FindAccount(UserName, Password);

            if (acc == null)
            {
                ModelState.AddModelError("", "UserName or Password was wrong");
                return(View(acc));
            }

            List <string> accRoles = new List <string>();

            for (int i = 0; i < acc.Roles.ToList().Count; i++)
            {
                accRoles.Add(acc.Roles.ToList()[i].Name);
            }

            //--------------My Principal--------------------

            CustomPrincipalSerializedModel serializeModel = new CustomPrincipalSerializedModel();

            serializeModel.ID       = acc.ID;
            serializeModel.FullName = acc.FullName;
            serializeModel.Roles    = accRoles.ToArray();

            JavaScriptSerializer selialier = new JavaScriptSerializer();

            string AccountData = selialier.Serialize(serializeModel);

            //----------------------------------

            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                1,
                acc.ID.ToString(),
                DateTime.Now,
                DateTime.Now.AddMinutes(90),
                false,
                AccountData,
                "/");

            //----------My Principal---------------

            string     encTiket = FormsAuthentication.Encrypt(authTicket);
            HttpCookie faCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTiket);

            Response.Cookies.Add(faCookie);

            //Using Principal : int UserID = (User as AuthoriziePrincipal).ID;

            if (accRoles.Contains("Admin"))
            {
                return(Redirect(returnURL ?? Url.Action("AccountsList", "Account")));
            }
            if (accRoles.Contains("Lecturer"))
            {
                return(Redirect(returnURL ?? Url.Action("SyllabusesList", "Syllabus")));
            }
            if (accRoles.Contains("Deanery"))
            {
                return(Redirect(returnURL ?? Url.Action("ProgramsList", "Program")));
            }
            if (accRoles.Contains("Editor"))
            {
                return(Redirect(returnURL ?? Url.Action("FaculitiesList", "Faculty")));
            }

            return(Redirect(returnURL ?? Url.Action("Index", "Home")));
        }