什么是与模拟用户权限在服务器路径写入文件? [英] What are user permission with impersonation to write file at server path?

查看:177
本文介绍了什么是与模拟用户权限在服务器路径写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used the example below but i have still have exception :

System.Security.Permissions.SecurityPermission
<强技术支持的企业人事>在这里,在下文中介绍的诊断信息:
请求类型System.Security.Permissions.SecurityPermission的'许可,mscorlib程序

管理员说,用户凭据具有完全权限读取和写入

Admin said the user credential has full permission to read and write

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

} 

您可以请帮助,因为我被困在这个问题?

Can you please help as I am stuck on this problem?

推荐答案

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

Your code does not run as full trust or did not get correct .Net permissions to perform PInvoke or access local files. It has yet nothing to do with Windows user permissions. The linked question will get you started on .Net CAS - Code Access Security - Why am I getting a System.Security.Permissions.SecurityPermission error in my .NET Application?.

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

One easy way to check is run your application locally on the machine in question for local drive - should get full trust and no longer getting CAS exceptions.

这篇关于什么是与模拟用户权限在服务器路径写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆