【问题标题】:How to pass credentials during writing file at server path?如何在服务器路径写入文件期间传递凭据?
【发布时间】:2017-01-23 05:44:42
【问题描述】:

我想在服务器路径上写一个文件,但是当我尝试这样做时,我们得到了异常,我们无权这样做。我们有一个有权在服务器路径上写入的应用程序 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();

【问题讨论】:

    标签: c# file-upload filestream


    【解决方案1】:
    [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
    }
    

    【讨论】:

    • 未编译当前上下文中不存在名称“外部”
    • 这个用户需要什么权限?因为我们仍然有例外
    • @thabet084 开始 - 让一个具有完全权限的用户并将他的详细信息放在上面的代码中。
    • 我在这里发布了详细信息stackoverflow.com/questions/10419841/…
    猜你喜欢
    • 2014-08-11
    • 2012-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 2012-08-26
    相关资源
    最近更新 更多