有没有人知道我如何设置访问不同的服务器文件夹与用户名和密码在c# .net

本文关键字:用户 文件夹 服务器 net 密码 何设置 没有人知道 访问 设置 | 更新日期: 2023-09-27 18:11:58

这里有一个简短的问题。有谁能告诉我如何在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;
           }
   }

有没有人知道我如何设置访问不同的服务器文件夹与用户名和密码在c# .net

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

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

这里有两篇文章,希望能对大家有所帮助,有样例代码:

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);
    }
}