1

我想在服务器路径上写一个文件,但是当我尝试这样做时,我们得到了异常,我们无权这样做。我们有一个有权在服务器路径上写入的应用程序 ID 和密码,但我不知道如何传递此凭据:

我当前的代码:

//Create a new GUID, extract the extension and create a new unique filename
string strFileGUID = System.Guid.NewGuid().ToString();
string extension = Path.GetExtension(attachment.AttachmentFileName);
string tempfilename = strFileGUID  + extension;  

string path = "ServerPath";

//Open a filestream and write the contents of the file at server path
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write );
fs.Write(fileContent.Content, 0, fileContent.Content.Length);
fs.Flush();
fs.Close();
4

1 回答 1

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

例子:

IntPtr userToken = IntPtr.Zero;

bool success = External.LogonUser(
  "john.doe", 
  "domain.com", 
  "MyPassword", 
  (int) AdvApi32Utility.LogonType.LOGON32_LOGON_INTERACTIVE, //2
  (int) AdvApi32Utility.LogonProvider.LOGON32_PROVIDER_DEFAULT, //0
  out userToken);

if (!success)
{
  throw new SecurityException("Logon user failed");
}

using (WindowsIdentity.Impersonate(userToken))
{
  // put your code here
}
于 2012-05-02T14:42:41.353 回答