Пример #1
0
        public bool LogIn(HttpResponseBase response, AuthenticateModel authParameters)
        {
            bool isLogged = false;

            if (CheckUser(authParameters.Login, authParameters.Password))
            {
                IDataBaseProvider   provider  = BaseBindings.DBFactory.CreateDBProvider();
                IUserAccessProvider uprovider = BaseBindings.DBFactory.CreateUserAccessProvider();

                UserModel user = new UserModel(uprovider);
                user.UserDetails = user.GetUserByEmailLogin(authParameters.Login);
                if (user != null)
                {
                    string accToken = uprovider.CreateAccessToken(user.UserDetails.Id);
                    _CookiesManager.SetToCookie(
                        response,
                        CookieType.UserAccessToken, accToken);
                    _CookiesManager.SetToCookie(
                        response,
                        CookieType.UserId, user.UserDetails.Id.ToString());
                    _CookiesManager.SetToCookie(
                        response,
                        CookieType.UserName, user.UserDetails.Name ?? user.UserDetails.Login);
                    isLogged = true;
                }
            }
            else
            {
                ClearCookies(response);
                authParameters.IsTrueUser          = false;
                authParameters.ErrorOnUserChecking = "User not found";
            }
            return(isLogged);
        }
Пример #2
0
        public void CreateAccessTokenTest()
        {
            MsSqlFactory        factory        = new MsSqlFactory(Common.Connectionconfig);
            MsSqlDataProvider   provider       = factory.CreateDBProvider() as MsSqlDataProvider;
            IUserAccessProvider accessProvider = factory.CreateUserAccessProvider();

            using (DbContext context = provider.GenerateContext())
            {
                DragonflyEntities ents = context as DragonflyEntities;
                decimal           createdAceessToken = 0;

                try
                {
                    decimal userId = provider.AddUser(_UserSignUpData);
                    Assert.IsTrue(userId > 0, "Error occured on the user save.");
                    string token        = accessProvider.CreateAccessToken(userId);
                    var    accessTokens = (from at in ents.User_Access
                                           where at.ID_User == userId
                                           select at);
                    if (accessTokens.Count() > 1)
                    {
                        foreach (var foundToken in accessTokens)
                        {
                            DeleteAccessTokenFromDB(context, foundToken.ID_User_Access);
                        }
                        Assert.Fail("Too many access tokens");
                    }
                    else if (accessTokens.Count() == 1)
                    {
                        createdAceessToken = accessTokens.First().ID_User_Access;
                        Assert.AreEqual(token, accessTokens.First().Access_Token);
                    }
                    else
                    {
                        Assert.Fail("Access tokens not found in the DB.");
                    }
                }
                finally
                {
                    DeleteUserFromDB(context, _UserSignUpData.Login, _UserSignUpData.EMail);
                    DeleteAccessTokenFromDB(context, createdAceessToken);
                }
            }
        }
Пример #3
0
        public void CheckAccessTokenTest()
        {
            MsSqlFactory        factory        = new MsSqlFactory(Common.Connectionconfig);
            MsSqlDataProvider   provider       = factory.CreateDBProvider() as MsSqlDataProvider;
            IUserAccessProvider accessProvider = factory.CreateUserAccessProvider();

            using (DbContext context = provider.GenerateContext())
            {
                DragonflyEntities ents = context as DragonflyEntities;
                try
                {
                    decimal userId = provider.AddUser(_UserSignUpData);
                    Assert.IsTrue(userId > 0, "Error occured on the user save.");
                    string token = accessProvider.CreateAccessToken(userId);
                    Assert.IsTrue(accessProvider.CheckAccessToken(userId, token));
                }
                finally
                {
                    DeleteUserFromDB(context, _UserSignUpData.Login, _UserSignUpData.EMail);
                }
            }
        }
Пример #4
0
        public void AccessTokenCascadeDeletionTest()
        {
            MsSqlFactory        factory        = new MsSqlFactory(Common.Connectionconfig);
            MsSqlDataProvider   provider       = factory.CreateDBProvider() as MsSqlDataProvider;
            IUserAccessProvider accessProvider = factory.CreateUserAccessProvider();

            using (DbContext context = provider.GenerateContext())
            {
                DragonflyEntities ents = context as DragonflyEntities;
                decimal           createdAceessToken = 0;

                try
                {
                    decimal userId = provider.AddUser(_UserSignUpData);
                    Assert.IsTrue(userId > 0, "Error occured on the user save.");
                    string token        = accessProvider.CreateAccessToken(userId);
                    var    accessTokens = (from at in ents.User_Access
                                           where at.ID_User == userId
                                           select at);
                    if (accessTokens.Count() == 1)
                    {
                        createdAceessToken = accessTokens.First().ID_User_Access;
                        DeleteUserFromDB(context, _UserSignUpData.Login, _UserSignUpData.EMail);
                        var tokensCount = (from at in ents.User_Access
                                           where at.ID_User == userId
                                           select at).Count();
                        Assert.AreEqual(0, tokensCount, "Access tokens for user stay in the DB");
                    }
                }
                finally
                {
                    DeleteUserFromDB(context, _UserSignUpData.Login, _UserSignUpData.EMail);
                    DeleteAccessTokenFromDB(context, createdAceessToken);
                }
            }
        }