Exemple #1
0
 public UserViewModel GetUserByUserNameAndPassword(string UserName, string Password)
 {
     UserModel objUserModel = new UserModel();
     UserViewModel objUserViewModel = new UserViewModel();
     try
     {
         objUserModel = objUserDB.GetUserByUserNameAndPassword(UserName, Password);
         Mapper.CreateMap<UserModel, UserViewModel>();
         objUserViewModel = Mapper.Map<UserModel, UserViewModel>(objUserModel);
     }
     catch (Exception ex)
     {
         throw;
     }
     return objUserViewModel;
 }
        private void SetAuthentication(UserViewModel user, bool RememberMe)
        {
            ContextViewModel CM = new ContextViewModel();
            CM.UserName = user.UserName;
            CM.UserId = user.Id;
            CM.RoleId = user.UserRoleId;
            AuthenticationSerialize serialiseAuth = new AuthenticationSerialize();
            UserContextViewModel userContext = new UserContextViewModel();
            userContext.CurrentUser = CM;
            serialiseAuth.UserContext = userContext;
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string userData = serializer.Serialize(serialiseAuth);

            var tenDaysFromNow = standardTime.AddDays(10);

            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(
                            1,
                             serialiseAuth.UserContext.CurrentUser.UserName + " " + serialiseAuth.UserContext.CurrentUser.UserName,
                             standardTime,
                             tenDaysFromNow,
                            RememberMe,
                            userData);

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

            if (RememberMe)
            {
                faCookie.Expires = System.DateTime.Now.AddMonths(1);
            }

            Response.Cookies.Add(faCookie);
        }