コード例 #1
0
        public AuthenticationResult SignIn(string username, string password)
        {
            PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
            bool             isAuthenticated  = false;
            UserPrincipal    userPrincipal    = null;

            try
            {
                isAuthenticated = principalContext.ValidateCredentials(username, password, ContextOptions.Negotiate);

                if (isAuthenticated)
                {
                    userPrincipal = UserPrincipal.FindByIdentity(principalContext, username);
                }
            }
            catch (Exception)
            {
                isAuthenticated = false;
                userPrincipal   = null;
            }

            if (!isAuthenticated || userPrincipal == null)
            {
                return(AuthenticationResult.FAILED("Username or Password is not correct."));
            }

            if (userPrincipal.IsAccountLockedOut())
            {
                return(AuthenticationResult.FAILED("Your account is locked."));
            }

            if (userPrincipal.Enabled.HasValue && userPrincipal.Enabled.Value == false)
            {
                return(AuthenticationResult.FAILED("Your account is disabled."));
            }

            ClaimsIdentity identity = CreateIdentity(userPrincipal);

            authenticationManager.SignOut(AUTHENTICATION.ApplicationCookie);
            authenticationManager.SignIn(new AuthenticationProperties()
            {
                IsPersistent = false
            }, identity);

            return(AuthenticationResult.SUCCESS());
        }