다음을 통해 공유


방법: WCF 인증 서비스를 사용하는 경우 사용자 로그인 사용자 지정

업데이트: 2007년 11월

이 항목에서는 WCF(Windows Communication Foundation)를 사용하여 ASP.NET 인증 서비스를 호출할 때 사용자 지정 자격 증명의 유효성을 검사하여 사용자를 인증하는 방법을 보여 줍니다. 인증에는 일반적으로 사용자 이름과 암호만 있으면 됩니다. 그러나 경우에 따라서는 ID 번호 등의 추가 자격 증명을 사용하여 사용자의 신원을 확인해야 할 수도 있습니다.

Java 응용 프로그램 같이 SOAP 1.1 메시지를 보내고 사용할 수 있는 클라이언트 응용 프로그램의 사용자를 로그인하려면 인증 서비스의 WCF 구현을 사용합니다.

인증을 위해 사용자 지정된 자격 증명의 유효성을 검사하려면

  1. 웹 응용 프로그램의 Global.asax 파일에 Authenticating 이벤트의 이벤트 처리기를 만듭니다.

  2. 처리기에서 해당 AuthenticatingEventArgs 매개 변수의 CustomCredential 속성 내용을 읽은 다음 값을 인증합니다.

    다음 예제에서는 CustomCredential 속성의 두 인증 값을 읽어 사용자 지정 인증 클래스인 StudentAuthentication으로 전달하는 방법을 보여 줍니다.

    Sub AuthenticationService_Authenticating _
       (ByVal sender As Object, _
        ByVal e As System.Web.ApplicationServices.AuthenticatingEventArgs)
        Dim studentid As String = String.Empty
        Dim answer As String = String.Empty
    
        Dim credentials As String() = _
             e.CustomCredential.Split(New Char() {","c})
        If (credentials.Length > 0) Then
            studentid = credentials(0)
            If (credentials.Length > 1) Then
                answer = credentials(1)
            End If
        End If
    
        Try
            e.Authenticated = _
                StudentAuthentication.ValidateStudentCredentials _
                (e.Username, e.Password, studentid, answer)
        Catch ex As ArgumentNullException
            e.Authenticated = False
        End Try
    
    
        e.AuthenticationIsComplete = True
    End Sub
    
    void AuthenticationService_Authenticating(object sender, System.Web.ApplicationServices.AuthenticatingEventArgs e)
    {
        string studentid = String.Empty;
        string answer = String.Empty;
    
        string[] credentials =
            e.CustomCredential.Split(new char[] { ',' });
        if (credentials.Length > 0)
        {
            studentid = credentials[0];
            if (credentials.Length > 1)
            {
                answer = credentials[1];
            }
        }
    
        try
        {
            e.Authenticated =
                StudentAuthentication.ValidateStudentCredentials
                (e.UserName, e.Password, studentid, answer);
        }
        catch (ArgumentNullException ex)
        {
            e.Authenticated = false;
        }
    
        e.AuthenticationIsComplete = true;
    }
    
  3. Global.asax 파일의 Application_Start 메서드에서 Authenticating 이벤트에 대해 이벤트 처리기를 바인딩합니다.

    다음 예제에서는 Authenticating 이벤트에 처리기를 바인딩하는 방법을 보여 줍니다.

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        AddHandler System.Web.ApplicationServices.AuthenticationService.Authenticating, _
          AddressOf Me.AuthenticationService_Authenticating
    End Sub
    
    void Application_Start(object sender, EventArgs e) 
    {
        System.Web.ApplicationServices.AuthenticationService.Authenticating += 
            new EventHandler<System.Web.ApplicationServices.AuthenticatingEventArgs>(AuthenticationService_Authenticating);
    
    }
    
  4. 웹 서비스의 SOAP 메시지를 사용할 수 있는 응용 프로그램에서 인증 서비스를 호출하고 CustomCredential 속성에서 인증할 추가 값을 전달합니다.

코드 컴파일

강력한 프로그래밍

이전 코드 예제에서는 한 개의 매개 변수라도 null인 경우 ArgumentNullException을 throw하는 사용자 지정 인증 클래스를 보여 줍니다. 사용자 코드에서는 유효성 검사 동안 발생하는 모든 예외를 처리해야 합니다.

보안

항상 SSL(Secure Sockets Layer) 및 HTTPS 프로토콜을 사용하여 인증 서비스를 액세스합니다.

참고 항목

개념

Windows Communication Foundation 인증 서비스 개요

참조

AuthenticationService

AuthenticatingEventArgs