티스토리 뷰

카테고리 없음

[씨#] ASP.Net MVC의 LDAP 인증

필살기쓰세요 2020. 12. 29. 16:44

이것은 웹 앱 양식 인증에서 수행하는 방법이므로 MVC에 대한 적응이 필요할 수 있습니다. asp.net 멤버십 및 역할 엔진을 사용합니다. Active Directory Membership 공급자를 사용하도록 공급자를 설정하고 인증을 위해 양식도 사용합니다.

<authentication mode="Forms">
  <forms name=".ADAuthCookie" 
           timeout="10"                     
                    loginUrl="Login.aspx" 
                             defaultUrl="Default.aspx">            
                               </forms>
                               

또는 그와 비슷한 것 ....

공급자 설정은 다음과 같습니다.

<membership defaultProvider="DomainLoginMembershipProvider">
  <providers>
      <add name="DomainLoginMembershipProvider"           
                   type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"           
                            connectionStringName="ADConnectionString"
                                     connectionProtection="Secure"
                                              connectionUsername="domainuser"
                                                       connectionPassword="pwd"
                                                                attributeMapUsername="sAMAccountName" 
                                                                         enableSearchMethods="false"/>
                                                                           </providers>
                                                                           </membership>
                                                                           

연결 보호, 사용자 이름 및 암호는 시스템을 대신하여 쿼리 AD에 액세스 할 수있는 계정을위한 것입니다. 네트워크 보안에 따라 설정해야 할 수도 있고 AD를 쿼리하여 사용자를 인증 할 수 없습니다.

연결 문자열은 다음과 같습니다.

<connectionStrings>
  <add name="ADConnectionString"
         connectionString="LDAP://servername:port#/DC=domainname"/>
         </connectionStrings>
         

연결 문자열은 다양한 형식을 취할 수 있으므로 환경에 맞게 조사해야 할 수 있습니다.

로그인 페이지의 경우 인증 방법을 실행하고 테스트해야 할 수 있습니다.

    e.Authenticated = Membership.ValidateUser(username, password);
    if (e.Authenticated == false)...
    

Stephen Shackow의 저서 "Professional ASP.Net 2.0 보안, 멤버십 및 역할 관리"는 AD 멤버십 사용에 대해 잘 설명합니다 (12 장). MVC의 맥락이 아니지만 구성 및 설정은 동일합니다.

-------------------

올바른 방향을 알려 주셔서 감사합니다.

        <authentication mode="Forms">
              <forms loginUrl="~/Account/LogOn" timeout="10"/>
                      </authentication>                  
                      
                       public bool ValidateUser(string userName, string password)
                               {
                                           bool validation;
                                                       try
                                                                   {
                                                                                   LdapConnection ldc = new LdapConnection(new LdapDirectoryIdentifier((string)null, false, false));
                                                                                                   NetworkCredential nc = new NetworkCredential(userName, password, "DOMAIN NAME HERE");
                                                                                                                   ldc.Credential = nc;
                                                                                                                                   ldc.AuthType = AuthType.Negotiate;
                                                                                                                                                   ldc.Bind(nc); // user has authenticated at this point, as the credentials were used to login to the dc.
                                                                                                                                                                   validation = true;
                                                                                                                                                                               }
                                                                                                                                                                                           catch (LdapException)
                                                                                                                                                                                                       {
                                                                                                                                                                                                                       validation = false;
                                                                                                                                                                                                                                   }
                                                                                                                                                                                                                                               return validation;
                                                                                                                                                                                                                                                       }
                                                                                                                                                                                                                                                       

사용자 유효성 검사가 성공했는지 확인하기 위해 try 블록에서 catch를 사용하고 있다는 사실이 마음에 들지 않지만 다른 방법을 찾을 수 없었습니다.

-------------------

참조한 블로그 게시물을 오해하고있는 것 같습니다. web.config 파일에 제공된 사용자 ID와 암호는 사용자가 제공 한 것이 아니라 ActiveDirectoryMembershipProvider가 AD에 연결하는 데 사용하는 것입니다. 본질적으로 그가 말하는 것은 SQL 멤버십 공급자를 AD 멤버쉽 공급자로 교체하고 작성된 코드를 사용하여 AD와 함께 작동하도록하는 것입니다. 이것이 바로 당신이해야 할 일입니다. 멤버십 공급자 코드를 전혀 사용하지 않으려면 관심있는 도메인의 주체 컨텍스트 에서 PrincipalContext.ValidateCredentials 메서드를 사용 하여 Login 메서드에 전달 된 자격 증명의 유효성을 검사 할 수 있습니다.

using (PrincipalContext context = new PrincipalContext( ContextType.Domain, "domain" )) {
    if (context.ValidateCredentials( username, password))
        {
                // log them in
                    }
                        else
                            {
                                   // set up error message and rerender view
                                       }
                                       }
                                       
-------------------

LdapConnection은 System.DirectoryServices.Protocols네임 스페이스 의 구성원입니다 (참조에 System.DirectoryServices.Protocols 라이브러리를 추가해야 함).

-------------------

System.Web.Security.ActiveDirectoryMembershipProvider.dll을 찾을 수 없습니다. 또한 나는 회원으로 검색하고 이것을 발견했습니다.

<membership defaultProvider="LdapMembershipProvider">
    <providers>
            <add name="LdapMembership"
                        type="Microsoft.Office.Server.Security.LDAPMembershipProvider,
                                    Microsoft.Office.Server,
                                                Version=12.0.0.0, Culture=neutral,
                                                            PublicKeyToken=71E9BCE111E9429C"
                                                                        server="DC"
                                                                                    port="389"
                                                                                                useSSL="false"
                                                                                                            userDNAttribute="distinguishedName"
                                                                                                                        userNameAttribute="sAMAccountName"
                                                                                                                                    userContainer="CN=Users,DC=userName,DC=local"
                                                                                                                                                userObjectClass="person"
                                                                                                                                                            userFilter="(|(ObjectCategory=group)(ObjectClass=person))"
                                                                                                                                                                        scope="Subtree"
                                                                                                                                                                                    otherRequiredUserAttributes="sn,givenname,cn"/>
                                                                                                                                                                                        </providers>
                                                                                                                                                                                        </membership>
                                                                                                                                                                                        


출처
https://stackoverflow.com/questions/1900588
댓글
공지사항
Total
Today
Yesterday
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30