本文介绍了如何在服务器路径书面方式文件中传递凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写在服务器路径的文件,但是当我试图做到这一点,我们得到了我们无权这样做例外。我们谁有权在服务器路径编写一个应用程序ID和密码,但我不知道我可以通过这个凭据:



我当前的代码:

  //创建一个新的GUID,提取信息并创建一个新的唯一的文件名
串strFileGUID = System.Guid.NewGuid( )的ToString();
线延长= Path.GetExtension(attachment.AttachmentFileName);
串tempfilename = strFileGUID +扩展名;

路径字符串=SERVERPATH;

//打开一个文件流,并在服务器的路径
的FileStream FS写入文件的内容=新的FileStream(路径,FileMode.Create,FileAccess.Write);
fs.Write(fileContent.Content,0,fileContent.Content.Length);
fs.Flush();
fs.Close();


解决方案

 函数[DllImport (ADVAPI32.DLL,SetLastError = TRUE)] 
公共静态的extern BOOL LogonUser的(
串lpszUsername,
串lpszDomain,
串lpszPassword,
INT dwLogonType,
INT dwLogonProvider,
OUT的IntPtr phToken);

示例:



  IntPtr的userToken = IntPtr.Zero; 

BOOL成功= External.LogonUser(
john.doe,
domain.com,
MyPassword输入
(INT) AdvApi32Utility.LogonType.LOGON32_LOGON_INTERACTIVE,// 2
(INT)AdvApi32Utility.LogonProvider.LOGON32_PROVIDER_DEFAULT,// 0
OUT userToken); (!成功)

如果
{
抛出新SecurityException异常(登录用户失败);使用(WindowsIdentity.Impersonate(userToken))
{
//这里把你的代码
}


}


I would like to write a file at server path but when I tried to do that we got exception that we have no permission to do that. We have an application ID and password who has permission to write at the server path but I do not know how can I pass this credentials :

My current code :

//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();
解决方案
[DllImport("advapi32.dll", SetLastError = true)]
    public static extern bool LogonUser(
            string lpszUsername,
            string lpszDomain,
            string lpszPassword,
            int dwLogonType,
            int dwLogonProvider,
            out IntPtr phToken);

Example:

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
}

这篇关于如何在服务器路径书面方式文件中传递凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 21:44