Пример #1
0
 public async void SignIn(QpUser user)
 {
     var principal      = GetClaimsPrincipal(user);
     var authProperties = new AuthenticationProperties();
     await _httpContext.SignInAsync(
         CookieAuthenticationDefaults.AuthenticationScheme,
         principal,
         authProperties);
 }
Пример #2
0
        public static QpUser GetUserInformationFromAuthenticationCookie(string userName)
        {
            var    context         = HttpContext.Current;
            QpUser userInformation = null;

            var userData = ((FormsIdentity)context.User.Identity).Ticket.UserData;

            if (userData.Length > 0)
            {
                userInformation = DeserializeUserInformation(userName, userData);
            }

            return(userInformation);
        }
Пример #3
0
        public static string SerializeUserInformation(QpUser userInformation)
        {
            var userData = string.Empty; // сериализованная информация о пользователе

            if (userInformation != null)
            {
                userData = userInformation.Id + "|" +
                           "|" +
                           userInformation.CustomerCode + "|" +
                           userInformation.LanguageId + "|" +
                           userInformation.CultureName + "|" +
                           userInformation.IsSilverlightInstalled + "|" +
                           string.Join(";", userInformation.Roles);
            }

            return(userData);
        }
Пример #4
0
        public static string CompleteAuthentication(QpUser user)
        {
            FormsAuthenticationTicket ticket;

            if (QPConfiguration.WebConfigSection.Authentication.AllowSaveUserInformationInCookie)
            {
                ticket = CreateAuthenticationTicket(user.Name, user);
            }
            else
            {
                ticket = CreateAuthenticationTicket(user.Name);
                AddUserInformationToStorage(user);
            }

            SetAuthenticationCookie(ticket);

            Logger.Log.Debug($"User successfully authenticated: {user.ToJsonLog()}");
            return(FormsAuthentication.GetRedirectUrl(string.Empty, false));
        }
Пример #5
0
        public static ClaimsPrincipal GetClaimsPrincipal(QpUser user)
        {
            List <Claim> claims = new List <Claim>
            {
                new Claim("Id", user.Id.ToString()),
                new Claim(ClaimTypes.Name, user.Name),
                new Claim(ClaimTypes.Role, string.Join(";", user.Roles)),
                new Claim("CustomerCode", user.CustomerCode ?? ""),
                new Claim("LanguageId", user.LanguageId.ToString()),
                new Claim("CultureName", user.CultureName),
                new Claim("MustChangePassword", user.MustChangePassword.ToString()),
                new Claim(ClaimTypes.Sid, user.SessionId.ToString())
            };

            var claimsIdentity = new ClaimsIdentity(
                claims, CookieAuthenticationDefaults.AuthenticationScheme);

            return(new ClaimsPrincipal(claimsIdentity));
        }
Пример #6
0
        public static QpUser DeserializeUserInformation(string userName, string userData)
        {
            QpUser userInformation = null;

            if (userName.Length > 0 && userData.Length > 0)
            {
                var userDataCollection = userData.Split('|');
                if (userDataCollection.Length > 0)
                {
                    userInformation = new QpUser
                    {
                        Id                     = int.Parse(userDataCollection[0]),
                        Name                   = userName,
                        CustomerCode           = userDataCollection[2],
                        LanguageId             = int.Parse(userDataCollection[3]),
                        IsSilverlightInstalled = bool.Parse(userDataCollection[5]),
                        Roles                  = userDataCollection[6].Split(';')
                    };
                }
            }

            return(userInformation);
        }
Пример #7
0
 public static FormsAuthenticationTicket CreateAuthenticationTicket(string userName, QpUser userInformation) => CreateAuthenticationTicket(userName, SerializeUserInformation(userInformation));
Пример #8
0
        public static void AddUserInformationToStorage(QpUser userInformartion)
        {
            var context = HttpContext.Current;

            context.Cache.Insert(userInformartion.Name, userInformartion, null, DateTime.Now.AddMinutes(30), Cache.NoSlidingExpiration);
        }