Exemplo n.º 1
0
        static public Membership.UserAuthenticateResults Login(string Email, string Password, bool GenerateRememberMeCookie)
        {
            Int64 UserId;

            Membership.UserAuthenticateResults results = Membership.AuthenticateUser(Email, Password, out UserId);
            if (results != Membership.UserAuthenticateResults.Success)
            {
                return(results);
            }

            UserAuthToken token = AuthTokens.GenerateAuthTokenForUserId(UserId, GenerateRememberMeCookie ? AuthTokenTimeSpan : 0);

            if (token == null)
            {
                return(Membership.UserAuthenticateResults.LoginError);
            }

            if (GenerateRememberMeCookie)
            {
                HttpCookie cookie = new HttpCookie(@"auth-token", TeaEncryptor.Encrypt(token.Secret.ToString(@"N") + @":" + token.Key, RememberMeCookieEncryptionKey));
                cookie.Expires = token.Expiry;
                HttpContext.Current.Response.Cookies.Add(cookie);
            }

            HttpContext.Current.Session[@"Authenticated"] = true;
            HttpContext.Current.Session[@"AuthTokenId"]   = token.UserAuthTokenId;
            HttpContext.Current.Session[@"UserId"]        = UserId;
            HttpContext.Current.Session[@"LangCode"]      = dg.Sql.Query.New <UserProfile>().Select(UserProfile.Columns.DefaultLangCode).Where(UserProfile.Columns.UserId, UserId).ExecuteScalar() as string;

            return(results);
        }