2012-07-18 5 views
0

응용 프로그램에서 폼 인증을 구현하려고합니다. 동시에 나는 서버에 대한 파일에 액세스하기 위해 orer에서 높은 사전 구축 된 계정을 사용하여 가장하려고합니다. 다음 코드를 작성했습니다 :폼 인증 및 가장

<system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    <authentication mode="Forms"> 
    <forms loginUrl="Login.aspx" timeout="30" defaultUrl="HomePage.aspx"   
    cookieless="AutoDetect"> 
     <credentials passwordFormat="Clear"> 
     <user name="user1" password="[email protected]"/> 
     <user name="user2" password="[email protected]"/> 
     </credentials> 
    </forms> 
    </authentication> 
    <authorization> 
    <deny users="?"/> 
    </authorization> 
    <identity impersonate="true" userName="domain\abcd" password="aaaa"/> 
    </system.web> 

가장이 보이지 않습니다. 양식 인증에 가장을 사용할 수 없습니까?

+0

예, 당신이있는 FormsAuthentication과 함께 가장을 사용할 수 있습니다. 정확히 당신이하려는 것은 작동하지 않는 것입니까? –

답변

-1

사용 양식 인증을 위해 다음

<forms loginUrl="Login.aspx" defaultUrl="HomePage.aspx" protection="Validation" timeout="30"/> 
0

로 가장 클래스 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Security; 
using System.Security.Principal; 
using System.Runtime.InteropServices; 
using System.IO; 
using System.Text; 

using System.Web; 

namespace [YourProgramName] //You must change it 
{ 
    public class Impersonate 
    { 

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

     [DllImport("kernel32.dll")] 
     private static extern int FormatMessage(int dwFlags, string lpSource, int dwMessageId, int dwLanguageId, 
               StringBuilder lpBuffer, int nSize, string[] Arguments); 


     private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 8; 
     private const int LOGON32_PROVIDER_DEFAULT = 0; 
     private const int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000; 

     private static WindowsImpersonationContext winImpersonationContext = null; 

     public static void ImpersonateUser(string domain, string userName, string password) 
     { 

      //Benutzer einloggen 
      int userToken = 0; 

      bool loggedOn = (LogonUser(userName, domain, password, LOGON32_LOGON_NETWORK_CLEARTEXT, 
             LOGON32_PROVIDER_DEFAULT, out userToken) != 0); 

      if (loggedOn == false) 
      { 
       int apiError = Marshal.GetLastWin32Error(); 
       StringBuilder errorMessage = new StringBuilder(1024); 
       FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, null, apiError, 0, errorMessage, 1024, null); 
       throw new Exception(errorMessage.ToString()); 
      } 

      WindowsIdentity identity = new WindowsIdentity((IntPtr)userToken); 
      winImpersonationContext = identity.Impersonate(); 

     } 

     public static void UndoImpersonation() 
     { 
      if (winImpersonationContext != null) 
      { 
       winImpersonationContext.Undo(); 
      } 
     } 

    } 
} 

이 프로그램을 사용

Impersonate.ImpersonateUser("Domain", "Username", "UserPassword"); 

        //Your Code as the new User 

       Impersonate.UndoImpersonation();