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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值