跨服务器上传文件的俩个例子

来源:互联网 发布:淘宝的快捷方式删不掉 编辑:程序博客网 时间:2024/05/20 02:23

1、添加<identity >标签

<system.web>

......

<identity impersonate="true" userName="域名/用户名" password="口令"></identity>

</system.web> 

 

2、(这种方法我没有测试成功,总是报1314错误)

 

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

       [DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    public extern static bool CloseHandle(IntPtr handle);

    [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
        int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);

IntPtr tokenHandle = new IntPtr(0);
   IntPtr dupeTokenHandle = new IntPtr(0);

   string[] args = new string[3];
   args[0] = "primos";
   args[1] = "066-wangxiaoming";
   args[2] = "1234";

   const int LOGON32_PROVIDER_DEFAULT = 0;
   //This parameter causes LogonUser to create a primary token.
   const int LOGON32_LOGON_INTERACTIVE = 2;
   //const int SecurityImpersonation = 2;

   
   tokenHandle = IntPtr.Zero;
   dupeTokenHandle = IntPtr.Zero;

   // Call LogonUser to obtain an handle to an access token.
   bool returnValue = LogonUser(args[1], args[0], args[2],
    LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
    ref tokenHandle);

   if (!returnValue)
   {
    
    GCom.SetClient_Script(Marshal.GetLastWin32Error().ToString());
   }

 

bool retVal = DuplicateToken(tokenHandle, SecurityImpersonation, ref dupeTokenHandle);

//添加逻辑代码

impersonatedUser.Undo();

// Free the tokens.
            if (tokenHandle != IntPtr.Zero)
                CloseHandle(tokenHandle);
            if (dupeTokenHandle != IntPtr.Zero)
                CloseHandle(dupeTokenHandle);

3、

using System.Security.Principal;
...
// Obtain the authenticated user's identity.
WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = null;
try
{
    // Start impersonating.
    ctx = winId.Impersonate();
    // Now impersonating.
    // Access resources using the identity of the authenticated user.
}
// Prevent exceptions from propagating.
catch
{
}
finally
{
    // Revert impersonation.
    if (ctx != null)
        ctx.Undo();
}
// Back to running under the default ASP.NET process identity.

原创粉丝点击