public void SetUp()
 {
     TestDataSetup.CreateTestData(Context);
     dbAuthProvider = (DbAuthenticationProvider)AuthenticationFactory.GetAuthenticationProvider("DbAuthenticationProviderName");
     password       = ASCIIEncoding.ASCII.GetBytes("Password");
     username       = "******";
 }
        public void CheckThatProviderFactoryCreatesConfiguredInstance()
        {
            dbAuthProvider = (DbAuthenticationProvider)AuthenticationFactory.GetAuthenticationProvider("DbAuthenticationProviderName");

            Assert.IsNotNull(dbAuthProvider);
            Assert.AreEqual("Microsoft.Practices.EnterpriseLibrary.Security.Database.Authentication.DbAuthenticationProvider", dbAuthProvider.ToString());
        }
예제 #3
0
        public int userLogin(User user)
        {
            int loginState = 0;

            byte[] userPassword = new SHA1Managed().ComputeHash(
                ASCIIEncoding.ASCII.GetBytes(user.UserPassword));

            //			manager.CreateUser(user.UserLoginID,userPassword);

            try
            {
                //				if(manager.UserExists(user.UserLoginID))
                //				{
                //					if(Convert.ToBase64String(userPassword).Equals
                //						(Convert.ToBase64String(manager.GetPassword(user.UserLoginID))))
                //					{
                NamePasswordCredential credentials;
                credentials = new NamePasswordCredential(user.UserLoginID,
                                                         ASCIIEncoding.ASCII.GetBytes(user.UserPassword));

                IAuthenticationProvider authProvider;
                authProvider = AuthenticationFactory.GetAuthenticationProvider("Database Provider");

                bool authenticated = false;

                IIdentity identity;
                authenticated = authProvider.Authenticate(credentials, out identity);

                if (authenticated)
                {
                    loginState = 1;                    //登陆成功

                    IRolesProvider rolesProvider = RolesFactory.GetRolesProvider("Role Database Provider");

                    IPrincipal principal = rolesProvider.GetRoles(identity);

                    Thread.CurrentPrincipal = principal;
                }
                //					}
                //					else
                //					{
                //						loginState = 2;//密码错误
                //					}
                //				}
                //				else
                //				{
                //					loginState = -1;//用户不存在
                //				}
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(loginState);
        }
        public void CheckIfAuthenticationSucceedsWithValidCredentialsThroughInterface()
        {
            IAuthenticationProvider IAuthProvider = AuthenticationFactory.GetAuthenticationProvider("DbAuthenticationProviderName");

            NamePasswordCredential credentials = new NamePasswordCredential(username, password);
            IIdentity identity;

            bool retVal = IAuthProvider.Authenticate(credentials, out identity);

            Assert.IsTrue(retVal);
            Assert.IsNotNull(identity);
            Assert.AreEqual(username, identity.Name);
            Assert.IsTrue(identity.IsAuthenticated);
        }
 public void CheckThatProviderFactoryDoesNotInitializeInvalidInstance()
 {
     AuthenticationFactory.GetAuthenticationProvider("NonConfiguredDbAuthenticationProviderName");
 }