Ejemplo n.º 1
0
        public void Validate()
        {
            var t1      = 1;
            var id1     = Guid.NewGuid();
            var login1  = "l1";
            var pwd1    = "p1";
            var it1     = 1;
            var expire1 = DateTime.UtcNow;
            var iu1     = 1;

            var cookie = CookieStorage.EncryptCookie(t1, id1, login1, pwd1, it1, expire1, iu1);

            int      t2;
            Guid     id2;
            string   login2;
            string   pwd2;
            int      it2;
            DateTime expire2;
            int      iu2;

            CookieStorage.DecryptCookie(cookie, out t2, out id2, out login2, out pwd2, out it2, out expire2, out iu2);

            Assert.AreEqual(t1, t2);
            Assert.AreEqual(id1, id2);
            Assert.AreEqual(login1, login2);
            Assert.AreEqual(pwd1, pwd2);
            Assert.AreEqual(it1, it2);
            Assert.AreEqual(expire1, expire2);
            Assert.AreEqual(iu1, iu2);
        }
Ejemplo n.º 2
0
        public static bool AuthenticateMe(string cookie)
        {
            if (!string.IsNullOrEmpty(cookie))
            {
                int    tenant;
                Guid   userid;
                string login;
                string password;
                if (CookieStorage.DecryptCookie(cookie, out tenant, out userid, out login, out password))
                {
                    if (tenant != CoreContext.TenantManager.GetCurrentTenant().TenantId)
                    {
                        return(false);
                    }

                    try
                    {
                        if (userid != Guid.Empty)
                        {
                            AuthenticateMe(new UserAccount(new UserInfo {
                                ID = userid
                            }, tenant));
                        }
                        else
                        {
                            AuthenticateMe(login, password);
                        }
                        return(true);
                    }
                    catch (InvalidCredentialException ice)
                    {
                        log.DebugFormat("{0}: cookie {1}, tenant {2}, userid {3}, login {4}, pass {5}",
                                        ice.Message, cookie, tenant, userid, login, password);
                    }
                    catch (SecurityException se)
                    {
                        log.DebugFormat("{0}: cookie {1}, tenant {2}, userid {3}, login {4}, pass {5}",
                                        se.Message, cookie, tenant, userid, login, password);
                    }
                    catch (Exception err)
                    {
                        log.ErrorFormat("Authenticate error: cookie {0}, tenant {1}, userid {2}, login {3}, pass {4}: {5}",
                                        cookie, tenant, userid, login, password, err);
                    }
                }
                else
                {
                    log.WarnFormat("Can not decrypt cookie: {0}", cookie);
                }
            }
            return(false);
        }
        public static bool AuthenticateMe(string cookie)
        {
            if (cookie == null)
            {
                throw new ArgumentNullException("cookie");
            }

            int    tenant;
            Guid   userid;
            string login;
            string password;

            if (CookieStorage.DecryptCookie(cookie, out tenant, out userid, out login, out password))
            {
                if (tenant != CoreContext.TenantManager.GetCurrentTenant().TenantId)
                {
                    return(false);
                }

                try
                {
                    if (userid != Guid.Empty)
                    {
                        AuthenticateMe(new UserAccount(new UserInfo {
                            ID = userid
                        }, tenant));
                    }
                    else
                    {
                        AuthenticateMe(login, password);
                    }
                    return(true);
                }
                catch (Exception err)
                {
                    LogManager.GetLogger("ASC.Core").ErrorFormat("Authenticate error: cookie {0}, tenant {1}, userid {2}, login {3}, pass {4}: {5}",
                                                                 cookie, tenant, userid, login, password, err);
                }
            }
            else
            {
                LogManager.GetLogger("ASC.Core").WarnFormat("Can not decrypt cookie: {0}", cookie);
            }
            return(false);
        }
Ejemplo n.º 4
0
        public void Validate()
        {
            var t1     = 1;
            var id1    = Guid.NewGuid();
            var login1 = "l1";
            var pwd1   = "p1";

            var cookie = CookieStorage.EncryptCookie(t1, id1, login1, pwd1);

            int    t2;
            Guid   id2;
            string login2;
            string pwd2;

            CookieStorage.DecryptCookie(cookie, out t2, out id2, out login2, out pwd2);

            Assert.AreEqual(t1, t2);
            Assert.AreEqual(id1, id2);
            Assert.AreEqual(login1, login2);
            Assert.AreEqual(pwd1, pwd2);
        }
Ejemplo n.º 5
0
        public static bool AuthenticateMe(string cookie)
        {
            if (cookie == null)
            {
                throw new ArgumentNullException("cookie");
            }

            int    tenant;
            Guid   userid;
            string login;
            string password;

            if (CookieStorage.DecryptCookie(cookie, out tenant, out userid, out login, out password))
            {
                if (tenant != CoreContext.TenantManager.GetCurrentTenant().TenantId)
                {
                    return(false);
                }

                try
                {
                    if (userid != Guid.Empty)
                    {
                        AuthenticateMe(new UserAccount(new UserInfo {
                            ID = userid
                        }, tenant));
                    }
                    else
                    {
                        AuthenticateMe(login, password);
                    }
                    return(true);
                }
                catch
                {
                }
            }
            return(false);
        }
Ejemplo n.º 6
0
        public void Validate(CookieStorage cookieStorage)
        {
            var t1      = 1;
            var id1     = Guid.NewGuid();
            var login1  = "l1";
            var pwd1    = "p1";
            var it1     = 1;
            var expire1 = DateTime.UtcNow;
            var iu1     = 1;

            var cookie = cookieStorage.EncryptCookie(t1, id1, login1, pwd1, it1, expire1, iu1);


            cookieStorage.DecryptCookie(cookie, out var t2, out var id2, out var login2, out var pwd2, out var it2, out var expire2, out var iu2);

            Assert.AreEqual(t1, t2);
            Assert.AreEqual(id1, id2);
            Assert.AreEqual(login1, login2);
            Assert.AreEqual(pwd1, pwd2);
            Assert.AreEqual(it1, it2);
            Assert.AreEqual(expire1, expire2);
            Assert.AreEqual(iu1, iu2);
        }
Ejemplo n.º 7
0
        public static bool AuthenticateMe(string cookie)
        {
            if (!string.IsNullOrEmpty(cookie))
            {
                int      tenant;
                Guid     userid;
                string   login;
                string   password;
                int      indexTenant;
                DateTime expire;
                int      indexUser;

                if (cookie.Equals("Bearer", StringComparison.InvariantCulture))
                {
                    var ipFrom  = string.Empty;
                    var address = string.Empty;
                    if (HttpContext.Current != null)
                    {
                        var request = HttpContext.Current.Request;
                        ipFrom  = "from " + (request.Headers["X-Forwarded-For"] ?? request.UserHostAddress);
                        address = "for " + request.GetUrlRewriter();
                    }
                    log.InfoFormat("Empty Bearer cookie: {0} {1}", ipFrom, address);
                }
                else if (CookieStorage.DecryptCookie(cookie, out tenant, out userid, out login, out password, out indexTenant, out expire, out indexUser))
                {
                    if (tenant != CoreContext.TenantManager.GetCurrentTenant().TenantId)
                    {
                        return(false);
                    }

                    var settingsTenant = TenantCookieSettings.GetForTenant(tenant);
                    if (indexTenant != settingsTenant.Index)
                    {
                        return(false);
                    }

                    if (expire != DateTime.MaxValue && expire < DateTime.UtcNow)
                    {
                        return(false);
                    }

                    try
                    {
                        if (userid != Guid.Empty)
                        {
                            var settingsUser = TenantCookieSettings.GetForUser(userid);
                            if (indexUser != settingsUser.Index)
                            {
                                return(false);
                            }

                            AuthenticateMe(new UserAccount(new UserInfo {
                                ID = userid
                            }, tenant));
                        }
                        else
                        {
                            AuthenticateMe(login, password);
                        }
                        return(true);
                    }
                    catch (InvalidCredentialException ice)
                    {
                        log.DebugFormat("{0}: cookie {1}, tenant {2}, userid {3}, login {4}, pass {5}",
                                        ice.Message, cookie, tenant, userid, login, password);
                    }
                    catch (SecurityException se)
                    {
                        log.DebugFormat("{0}: cookie {1}, tenant {2}, userid {3}, login {4}, pass {5}",
                                        se.Message, cookie, tenant, userid, login, password);
                    }
                    catch (Exception err)
                    {
                        log.ErrorFormat("Authenticate error: cookie {0}, tenant {1}, userid {2}, login {3}, pass {4}: {5}",
                                        cookie, tenant, userid, login, password, err);
                    }
                }
                else
                {
                    var ipFrom  = string.Empty;
                    var address = string.Empty;
                    if (HttpContext.Current != null)
                    {
                        var request = HttpContext.Current.Request;
                        address = "for " + request.GetUrlRewriter();
                        ipFrom  = "from " + (request.Headers["X-Forwarded-For"] ?? request.UserHostAddress);
                    }
                    log.WarnFormat("Can not decrypt cookie: {0} {1} {2}", cookie, ipFrom, address);
                }
            }
            return(false);
        }
Ejemplo n.º 8
0
        public static bool AuthenticateMe(string cookie)
        {
            if (!string.IsNullOrEmpty(cookie))
            {
                int      tenant;
                Guid     userid;
                string   login;
                string   password;
                int      indexTenant;
                DateTime expire;
                int      indexUser;

                if (CookieStorage.DecryptCookie(cookie, out tenant, out userid, out login, out password, out indexTenant, out expire, out indexUser))
                {
                    if (tenant != CoreContext.TenantManager.GetCurrentTenant().TenantId)
                    {
                        return(false);
                    }

                    var settingsTenant = TenantCookieSettings.GetForTenant(tenant);
                    if (!settingsTenant.IsDefault() && indexTenant != settingsTenant.Index)
                    {
                        return(false);
                    }

                    if (expire != DateTime.MaxValue && expire < DateTime.UtcNow)
                    {
                        return(false);
                    }

                    try
                    {
                        if (userid != Guid.Empty)
                        {
                            var settingsUser = TenantCookieSettings.GetForUser(userid);
                            if (!settingsUser.IsDefault() && indexUser != settingsUser.Index)
                            {
                                return(false);
                            }

                            AuthenticateMe(new UserAccount(new UserInfo {
                                ID = userid
                            }, tenant));
                        }
                        else
                        {
                            AuthenticateMe(login, password);
                        }
                        return(true);
                    }
                    catch (InvalidCredentialException ice)
                    {
                        log.DebugFormat("{0}: cookie {1}, tenant {2}, userid {3}, login {4}, pass {5}",
                                        ice.Message, cookie, tenant, userid, login, password);
                    }
                    catch (SecurityException se)
                    {
                        log.DebugFormat("{0}: cookie {1}, tenant {2}, userid {3}, login {4}, pass {5}",
                                        se.Message, cookie, tenant, userid, login, password);
                    }
                    catch (Exception err)
                    {
                        log.ErrorFormat("Authenticate error: cookie {0}, tenant {1}, userid {2}, login {3}, pass {4}: {5}",
                                        cookie, tenant, userid, login, password, err);
                    }
                }
                else
                {
                    log.WarnFormat("Can not decrypt cookie: {0}", cookie);
                }
            }
            return(false);
        }