0

这里只是一个简单的问题。有谁可以在 c#.net 中使用用户名和密码设置访问不同的服务器文件夹?

我有以下代码可以将文件上传到另一台服务器的文件夹中。不知何故,我需要使用用户名和密码,然后我只能访问这个文件夹。有谁知道设置文件夹访问用户名和密码的代码?

   private void uploadFile()
   {

           DateTime dateTime = DateTime.Now;
           string sDate = dateTime.ToString("yyyyMMdd") + "_" + dateTime.ToString("fffffff");

           string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);

           string SaveLocation = Server.MapPath("document") + "\\" + sDate + "_" + fn;

           supportFileName = sDate + "_" + fn;

           try
           {
               File1.PostedFile.SaveAs(SaveLocation);
           }
           catch (Exception ex)
           {
               err.Text = "Error: " + ex.Message;
           }
   }
4

1 回答 1

2

用于访问不同的服务器,并将文件上传到其中。首先,您需要赋予此用户帐户正确的权限,并模拟此帐户。

编程上下文中的术语“模拟”是指在与最初启动应用程序的用户不同的用户上下文下执行代码的技术,即在应用程序执行期间用户上下文临时更改一次或多次。

这里有两篇文章,希望可以帮助你,示例代码:

http://www.codeproject.com/Articles/4051/Windows-Impersonation-using-C

http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User

    using ( new Impersonator( "myUsername", "myDomainname", "myPassword" ) )
{
   ...

   <code that executes under the new context>

   ...
}





 public WindowsImpersonationContext 
    ImpersonateUser(string sUsername, string sDomain, string sPassword)
{
    // initialize tokens
    IntPtr pExistingTokenHandle = new IntPtr(0);
    IntPtr pDuplicateTokenHandle = new IntPtr(0);
    pExistingTokenHandle = IntPtr.Zero;
    pDuplicateTokenHandle = IntPtr.Zero;

    // if domain name was blank, assume local machine
    if (sDomain == "")
        sDomain = System.Environment.MachineName;

    try
    {
        string sResult = null;

        const int LOGON32_PROVIDER_DEFAULT = 0;

        // create token
        const int LOGON32_LOGON_INTERACTIVE = 2;
        //const int SecurityImpersonation = 2;

        // get handle to token
        bool bImpersonated = LogonUser(sUsername, sDomain, sPassword, 
            LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, 
                ref pExistingTokenHandle);

        // did impersonation fail?
        if (false == bImpersonated)
        {
            int nErrorCode = Marshal.GetLastWin32Error();
            sResult = "LogonUser() failed with error code: " + 
                nErrorCode + "\r\n";

            // show the reason why LogonUser failed
            MessageBox.Show(this, sResult, "Error", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        // Get identity before impersonation
        sResult += "Before impersonation: " + 
            WindowsIdentity.GetCurrent().Name + "\r\n";

        bool bRetVal = DuplicateToken(pExistingTokenHandle, 
            (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, 
                ref pDuplicateTokenHandle);

        // did DuplicateToken fail?
        if (false == bRetVal)
        {
            int nErrorCode = Marshal.GetLastWin32Error();
            // close existing handle
            CloseHandle(pExistingTokenHandle); 
            sResult += "DuplicateToken() failed with error code: " 
                + nErrorCode + "\r\n";

            // show the reason why DuplicateToken failed
            MessageBox.Show(this, sResult, "Error", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return null;
        }
        else
        {
            // create new identity using new primary token
            WindowsIdentity newId = new WindowsIdentity
                                        (pDuplicateTokenHandle);
            WindowsImpersonationContext impersonatedUser = 
                                        newId.Impersonate();

            // check the identity after impersonation
            sResult += "After impersonation: " + 
                WindowsIdentity.GetCurrent().Name + "\r\n";

            MessageBox.Show(this, sResult, "Success", 
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            return impersonatedUser;
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        // close handle(s)
        if (pExistingTokenHandle != IntPtr.Zero)
            CloseHandle(pExistingTokenHandle);
        if (pDuplicateTokenHandle != IntPtr.Zero) 
            CloseHandle(pDuplicateTokenHandle);
    }
}
于 2013-05-02T05:01:59.563 回答