什么是使用模拟用户权限在服务器路径写入文件?(What are user permission wi

2019-09-16 23:45发布

我用下面的例子,但我都还是有例外:

System.Security.Permissions.SecurityPermission 这里在下文中介绍的技术支持企业人事诊断信息:请求类型的权限“System.Security.Permissions.SecurityPermission,mscorlib中

管理员说,用户证书具有完全的权限读取和写入

IntPtr userToken = IntPtr.Zero; 
bool success = External.LogonUser( 
    "userID",  
    "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)) 
{ 
    //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(); 

} 

你能帮帮我我坚持这个问题?

Answer 1:

您的代码不运行的完全信任或没有得到正确的.Net权限执行的PInvoke或访问本地文件。 它尚未无关的Windows用户权限。 链接的问题将让你开始净CAS -代码访问安全- 为什么我在我的.NET应用程序得到一个System.Security.Permissions.SecurityPermission错误? 。

一个简单的检查方法是在本地驱动器该计算机上本地运行的应用程序 - 应该得到充分的信任,不再获得CAS例外。



文章来源: What are user permission with impersonation to write file at server path?
标签: c# filestream