2013-03-25 1 views
10

로컬 컴퓨터 자격 증명에 대해 사용자 이름과 암호의 유효성을 검사하는 Login 메서드가 포함 된 WCF 서비스가 있는데, 일부 사용자의 경우 일정하지 않은 시간이 지나면 작동이 중지됩니다.PrincipalContext.ValidateCredentials를 사용하여 로컬 시스템에 인증 할 때 오류가 발생합니까?

실제 로그인 명령은 다음과 같습니다

public UserModel Login(string username, string password, string ipAddress) 
{ 
    // Verify valid parameters 
    if (username == null || password == null) 
     return null; 

    try 
    { 
     using (var pContext = new PrincipalContext(ContextType.Machine)) 
     { 
      // Authenticate against local machine 
      if (pContext.ValidateCredentials(username, password)) 
      { 
       // Authenticate user against database 
       using (var context = new MyEntities(Connections.GetConnectionString())) 
       { 
        var user = (from u in context.Users 
           where u.LoginName.ToUpper() == username.ToUpper() 
             && u.IsActive == true 
             && (string.IsNullOrEmpty(u.AllowedIpAddresses) 
             || u.AllowedIpAddresses.Contains(ipAddress)) 
           select u).FirstOrDefault(); 

        // If user failed to authenticate against database 
        if (user == null) 
         return null; 

        // Map entity object to return object and assign session token 
        var userModel = Mapper.Map<User, UserModel>(user); 
        userModel.Token = Guid.NewGuid(); 
        userModel.LastActivity = DateTime.Now; 

        // Authenticated users are added to a list on the server 
        // and their login expires after 20 minutes of inactivity 
        authenticatedUsers.Add(userModel); 
        sessionTimer.Start(); 

        // User successfully authenticated, so return UserModel to client 
        return userModel; 
       } 
      } 
     } 
    } 
    catch(Exception ex) 
    { 
     // This is getting hit 
     MessageLog.WriteMessage(string.Format("Exception occurred while validating user: {0}\n{1}", ex.Message, ex.StackTrace)); 
     return null; 
    } 

    // If authentication against local machine failed, return null 
    return null; 
} 

이 몇 일 동안 잘 작동하는 표시, 그것은 갑자기 일부 사용자에 대해 작업 중지가 발생합니다이 예외 :

두 개 이상의 사용자 이름을 사용하여 동일한 사용자가 서버 또는 공유 리소스에 여러 번 연결할 수 없습니다. 서버 또는 공유 리소스에 대한 이전의 모든 연결을 끊고 다시 시도하십시오. System.DirectoryServices.AccountManagement.CredentialValidator.Validate에서 System.DirectoryServices.AccountManagement.CredentialValidator.BindSam (문자열 대상 문자열 이름, 문자열 암호)

(문자열의 userName에서

, (0x800704C3 HRESULT에서 예외) 문자열 암호) C에서 System.DirectoryServices.AccountManagement.PrincipalContext.ValidateCredentials (문자열 이름, 문자열 암호)

MyNamespace.LoginService.Login에서

(문자열 이름, 문자열 암호 문자열 IPADDRESS)에서

: \ 사용자 \ 엠 전자 \ Desktop은 \ somefolder는 LoginService.svc.cs : \ 라인 67

행 67 : if (pContext.ValidateCredentials(username, password))

이 문제인지 아닌지 나는 잘 모르겠지만, 오류 메시지의 마지막 줄이있다 경로를 내 개발 컴퓨터에서 VS 솔루션의 프로덕션 서버에 파일 경로가 아닙니다.

실패 할 경우 일부 사용자 만 실패하지만 다른 사용자는 정상적으로 계속 로그인 할 수 있습니다. 오류를 일시적으로 해결할 수있는 유일한 방법은 iisreset입니다. 웹 사이트를 중지/시작하거나 앱 풀을 다시 사용할 수 없습니다.

요청시 오류를 재현 할 수 없습니다. 여러 세션 및 IP 주소에서 동일한 사용자로 로그인을 시도했습니다. 동일한 브라우저 세션의 다른 사용자와 동시에 로그인하고 로그인 버튼을 스팸으로 보내 여러 번 실행하도록 시도했지만 모든 것이 나타납니다. 잘 작동합니다.

I 사용자가 과거에 로그인 할 성공적으로 수 있었던 것을 우리의 기록에서 볼 수 있습니다

 
3/21/2013 
o 9:03a I logged in 
o 1:54p UserB logged in 
o 1:55p UserA logged in 
o 2:38p UserB logged in 
o 3:48p UserB logged in 
o 5:18p UserA logged in 
o 6:11p UserB logged in 

3/22/2013 
o 12:42p UserA logged in 
o 5:22p UserB logged in 
o 8:04p UserB logged in 

3/25/2013 (today) 
o 8:47a I logged in 
o 12:38p UserB tries logging in and fails. Repeated ~15 times over next 45 min 
o 1:58p I login successfully 
o 2:08p I try to login with UserB's login info and fail with the same error 

사용자가 FTP 액세스를 위해 로컬로 만든 계정을 가지고 있기 때문에 우리가 로컬 컴퓨터에 대한 인증을하는 이유는,과 우리는 우리 자신의 사용자 정의 로그인 시스템을 구축하거나 사용자가 두 가지 자격 증명 세트를 기억하도록하지 않았습니다.

코드는 사용자 자격 증명 만 인증해야하며 사용자 자격 증명으로 다른 작업을 수행하지 않아야합니다. System.DirectoryServices을 사용하는 다른 코드는없고 파일 입출력이없고 웹 응용 프로그램을 실행하는 데 필요한 파일 이외의 파일 시스템에 로컬로 액세스 할 수 없습니다.

며칠 후에 그 오류가 무작위로 나타나는 이유는 무엇입니까? 어떻게 해결할 수 있습니까?

서버 IIS 6.0을 실행하는 윈도우 서버 2003이며,이 문제가 this forum post입니다 설명으로 온라인 닷넷 프레임 워크 4.0

답변

4

내가 찾을 수있는 가장 가까운를 사용하는 설정입니다 같은 오류가 발생하는 사용자

WinNT 공급자는 서버 환경에서 제대로 작동하지 않습니다. 나는 실제로 이라는 사실에 놀라지 않을 것이다. 2 또는 3 사용자 만이 을 (를) 획득 할 수 있습니다. 쓰기 @stephbu로 올바르게 사람을 인증하는 가장 좋은 방법을

진술

this SO comment는 LogonUserAPI 를 사용하는 것입니다. 이 게시물에 설명 된 모든 다른 방법은하지 않습니다 WORK 100 % "다른 모든 방법이"상단이 PrincipalContext.ValidateCredentials 같은 소리 PrincipalContext.ValidateCredentials

그것을 사용하는 대답을 투표를 포함

Windows에서 완전히 100 % 신뢰할 수 없습니다 Server 2003 및 IIS6.0이므로 인증 코드를 다시 작성하여 LogonUser WinAPI 메서드를 대신 사용했습니다.

[DllImport("advapi32.dll", SetLastError = true)] 
public static extern bool LogonUser(
    string lpszUsername, 
    string lpszDomain, 
    string lpszPassword, 
    int dwLogonType, 
    int dwLogonProvider, 
    out IntPtr phToken 
    ); 

IntPtr hToken; 
if (LogonUser(username, "", password, 
    LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, out hToken)) 
{ 
    ... 
} 
+0

나는 몇 시간을 낭비했습니다. 감사합니다. rachel, –

관련 문제