Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

Working with Identity Impersonate from code behind

$
0
0

I have an application where I'm storing files into a network Folder. I have set the permission of the folder to write for "Everyone". Still its showing me "Unauthorized access". So, I thought of going withIdentity. I cannot write my username and password in the web.config since it will be readable. So, I'm trying to override the data with Username and password from code behind. But when i run the application it doesnt work and says "Unauthorized access". Here are the steps which I have followed.

In web.config i have set

<identity impersonate="false" />

Now from coding I'm changing identity properties like this.

System.Configuration.Configuration configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/");
System.Web.Configuration.IdentitySection id = (System.Web.Configuration.IdentitySection)configuration.GetSection("system.web/identity"); // new System.Web.Configuration.IdentitySection();
id.Impersonate = true;
id.UserName=@"domainname\admin";
id.Password="abcdefhipassword";
MyFileUpload.SaveAs(fullpath);

I don't know whether this will override it[When we debug it shows the new data as the IdentitySection]. But when I read MSDN its written we can override the username and password. I referred to the links below and reached upto here. But it doesn't work. I'm again getting the same error.

http://msdn.microsoft.com/en-us/library/system.web.configuration.identitysection.impersonate(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.web.configuration.identitysection.username(v=vs.110).aspx

If we add the Identity Impersonate section with username and password in the web.config it works perfectly.

Do anyone know how to write this from code behind.

NOTE: I have referred other links showing identity impersonation which is quite big with some dllimport etc etc. Without this can we implement.


Viewing all articles
Browse latest Browse all 4737