Cannot write on a mapped drive using impersonation

泄露秘密 提交于 2019-12-02 04:36:29

I couldn't get that to work either.

Then I realized that even if I could implement it, there is an easier way. What I did was share the folder on the target machine, and give only read/write permissions to the users that would be using my application.

//Impersonate user to save file on server
WindowsIdentity wi = (WindowsIdentity)User.Identity;
WindowsImpersonationContext wic = null;

try
{
    wic = wi.Impersonate();
    if (wi.IsAuthenticated)
        asyncFileUpload.SaveAs(location);
}
catch (Exception ex)
{
    //Log Error or notify here
    success = false;
}
finally
{
    if (wic != null)
        wic.Undo();
}

I created an AD group for the users, and give read/write permissions for those users on the hidden shared drive. This makes it easier to maintain, since I don't have to create mapped drives for each user.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!