2012-09-14 4 views
10

코드 블라인드 순간이 있습니다.web.config에 저장된 사용자를 구현하는 멤버 자격 공급자는 무엇입니까?

ASP.NET 4.0.

의 Web.config :

<?xml version="1.0"?> 
<configuration> 
    <system.web> 
    <authentication mode="Forms"> 
     <forms name="DataViewer" loginUrl="login.aspx"> 
     <credentials passwordFormat="Clear"> 
      <user name="devuser" password="test" /> 
     </credentials> 
     </forms> 
    </authentication> 
    <authorization> 
     <deny users="?" /> 
    </authorization> 
    </system.web> 

및 로그인 제어 :

<asp:Login ID="login" runat="server" /> 

내가 사용자 이름과 암호를 입력하고 로그인을 클릭하면이 정지됩니다.

내가 깨면, login.AuthenticateUsingMembershipProvider()SqlMembershipProvider.ValidateUser()을 호출하는 중임을 호출 스택에서 볼 수 있습니다. 이 프로젝트에는 데이터베이스가 정의되거나 포함되어 있지 않으며 SqlMembershipProvider를 사용해야한다고 지정하지 않았습니다.

내 질문에 <credentials> 요소의 사용자 이름과 암호를 ASP.NET이 사용하게하려면 어떤 멤버 공급자를 사용해야합니까? web.config?

+0

될 수 있기 때문에 기본 멤버 자격 공급자 는 사용자 저장소로 SQL Server 데이터베이스를 사용하는 AspNetSqlProvider입니다. – Scorpio

+0

이것이 임시 해결책이라고 기대하고 있습니다. web.config에서 자격 증명을 유지 관리하는 것은 매우 어려워지며 각각의 변경으로 인해 웹 응용 프로그램이 다시 시작됩니다. – jrummell

+0

물론. 그러나 그것은 현재의 시나리오에 적합합니다.암호가 절대로 바뀌지 않는 단일 사용자를 위해 간단한 응용 프로그램에 사소한 보호 기능을 제공해야합니다. 더 큰 의존성을 가진 솔루션은 적절하지 않습니다. – tomfanning

답변

14

프레임 워크 설계자가 <credentials /> 요소를 정의하는 문제에 어떻게 소비했는지 고려해 보면 놀랍습니다.

내가 수정하여 아래에 포함 된 here의 일종의 작업 구현을 발견했습니다. MembershipProvider의 다른 모든 구성원은 NotImplementedException을 던집니다. 당신이 시도했지만 한 경우 잘 모르겠어요

using System.Configuration; 
using System.Web.Configuration; 
using System.Web.Security; 

public class WebConfigMembershipProvider : MembershipProvider 
{ 
    private FormsAuthenticationUserCollection _users = null; 
    private FormsAuthPasswordFormat _passwordFormat; 

    public override void Initialize(string name, 
     System.Collections.Specialized.NameValueCollection config) 
    { 
     base.Initialize(name, config); 
     _passwordFormat = getPasswordFormat(); 
    } 

    public override bool ValidateUser(string username, string password) 
    { 
     var user = getUsers()[username]; 
     if (user == null) return false; 

     if (_passwordFormat == FormsAuthPasswordFormat.Clear) 
     { 
      if (user.Password == password) 
      { 
       return true; 
      } 
     } 
     else 
     { 
      if (user.Password == FormsAuthentication.HashPasswordForStoringInConfigFile(password, 
       _passwordFormat.ToString())) 
      { 
       return true; 
      } 
     } 

     return false; 
    } 

    protected FormsAuthenticationUserCollection getUsers() 
    { 
     if (_users == null) 
     { 
      AuthenticationSection section = getAuthenticationSection(); 
      FormsAuthenticationCredentials creds = section.Forms.Credentials; 
      _users = section.Forms.Credentials.Users; 
     } 
     return _users; 
    } 

    protected AuthenticationSection getAuthenticationSection() 
    { 
     Configuration config = WebConfigurationManager.OpenWebConfiguration("~"); 
     return (AuthenticationSection)config.GetSection("system.web/authentication"); 
    } 

    protected FormsAuthPasswordFormat getPasswordFormat() 
    { 
     return getAuthenticationSection().Forms.Credentials.PasswordFormat; 
    } 
} 
+1

당신은 젠체하는 사람이야! – sbeskur

+0

환영합니다 :-) – tomfanning

+0

나는 Deprecated Method FormsAuthentication.Authenticate를 사용하려고했습니다. 여기 수업을 해줘서 고마워. – strider

3

당신은 이것을 위해 당신 자신의 공급자를 써야 할 것입니다. MSDN documentation에서 샘플 ReadOnlyXmlMembershipProvider을 가져 와서 외부 XML 파일 대신 사용자 및 자격 증명을 web.config에서 읽도록 변경하는 것이 비교적 간단해야합니다.

2

....

FormsAuthentication.Authenticate 권장 동작은 회원을 사용하는 것입니다 때문에 지금은 사용되지 않지만 (당신을 위해 할 책임이다 오브젝트) MSDN 가입일

:

인증 용 방법은 애플리케이션 구성 파일의 자격 부에 저장된 사용자의 자격 증명을 확인. 또는 ASP.NET 멤버 자격을 사용하여 사용자 자격 증명을 저장하고 ValidateUser를 호출하여 자격 증명을 확인할 수 있습니다.

(당신이 당신의 Web.config에 그들을 선언하지 않는 경우에도, 그들은 machine.config 파일에서 상속되기 때문에) 당신은 또한 멤버 자격 공급자를 제거 할 수 있습니다

<membership> 
    <providers> 
     <remove name="AspNetSqlMembershipProvider"/> 
    </providers> 
    </membership> 
관련 문제