Example #1
0
        public static string Login(HttpContext context, Guid userId, DateTime?modifiedOn, bool rememberMe)
        {
            var identity = CreateIdentity(userId);

            if (identity == null)
            {
                throw new Exception("Try to login with invalid user.");
            }

            if (modifiedOn != identity.User.ModifiedOn)
            {
                modifiedOn = identity.User.ModifiedOn;
            }



            ErpUser user  = new SecurityManager().GetUser(userId);
            string  token = AuthToken.Create(user, rememberMe).Encrypt();

            if (rememberMe)
            {
                CookieOptions options = new CookieOptions();
                options.Expires = DateTime.Today.AddDays(AUTH_REMEMBER_IDENTITY_DAYS);
                context.Response.Cookies.Append(AUTH_TOKEN_KEY, token, options);
            }
            else
            {
                context.Response.Cookies.Append(AUTH_TOKEN_KEY, token);
            }

            context.User = new ErpPrincipal(identity);

            new SecurityManager().UpdateUserLastLoginTime(userId);

            return(token);
        }