问题
I used the example below but i have still have exception :
System.Security.Permissions.SecurityPermission Here below is presented diagnostic info for technical support personel: Request for the permission of type '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?
回答1:
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?.
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.
来源:https://stackoverflow.com/questions/10419841/what-are-user-permission-with-impersonation-to-write-file-at-server-path