Exemplo n.º 1
0
        /// <summary>
        /// Logins the specified user name and then generate a token for SSO.
        /// </summary>
        /// <param name="userName">The user name or email or cell phone no.</param>
        /// <param name="password">The password for the user.</param>
        /// <returns><c>true</c> if the user was logged in; otherwise, <c>false</c>.</returns>
        public static string LoginAndCreateSSOToken(string userNameOrEmailOrCellPhoneNo, string password)
        {
            var provider = VerifyProvider();

            bool success = false;

            LoginIdentityType identityType = GetLoginIdentityType(userNameOrEmailOrCellPhoneNo);

            if (identityType == LoginIdentityType.UserName)
            {
                success = provider.ValidateUser(userNameOrEmailOrCellPhoneNo, password);
            }
            else if (identityType == LoginIdentityType.Email)
            {
                success = provider.ValidateUserByEmail(userNameOrEmailOrCellPhoneNo, password);
            }
            else if (identityType == LoginIdentityType.CellPhoneNo)
            {
                success = provider.ValdateUserByCellPhoneNo(userNameOrEmailOrCellPhoneNo, password);
            }

            if (success)
            {
                FormsAuthenticationTicket authenticationTicket = new FormsAuthenticationTicket(
                    1,
                    userNameOrEmailOrCellPhoneNo,
                    DateTime.Now,
                    DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes),
                    true,
                    Request.UserHostAddress);

                string     encryptedTicket = FormsAuthentication.Encrypt(authenticationTicket);
                HttpCookie ticketCookie    = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
                ticketCookie.Domain = FormsAuthentication.CookieDomain;
                Response.Cookies.Add(ticketCookie);

                // Create a token for SSO passport and then add to token management.
                string token = RdbmsWebSecurity.CreatePassportToken();

                ObjectsMapper <FormsAuthenticationTicket, PassportAuthenticationTicket> mapper =
                    ObjectMapperManager.DefaultInstance.GetMapper <FormsAuthenticationTicket, PassportAuthenticationTicket>();

                PassportAuthenticationTicket passportTicket = mapper.Map(authenticationTicket);

                PassportTokenManager.Instance.AddToken(token, passportTicket, DateTime.Now.AddMinutes(FormsAuthentication.Timeout.Minutes));

                return(token);
            }

            return(null);
        }
Exemplo n.º 2
0
 public void AddToken(string token, PassportAuthenticationTicket credential, DateTime expire)
 {
     using (IRedisClient redisClient = this.CreateRedisClient())
     {
         if (!redisClient.ContainsKey(token))
         {
             PassportTokenItem tokenItem = new PassportTokenItem(token, credential, expire);
             redisClient.Set <PassportTokenItem>(token, tokenItem, expire);
         }
         else
         {
             PassportTokenItem tokenItem = redisClient.Get <PassportTokenItem>(token);
             tokenItem.Expire = expire;
             redisClient.Set <PassportTokenItem>(token, tokenItem, expire);
         }
     }
 }
Exemplo n.º 3
0
 public PassportTokenItem(string token, PassportAuthenticationTicket credential, DateTime expire)
 {
     this.Token      = token;
     this.Credential = credential;
     this.Expire     = expire;
 }