views:

58

answers:

1

Basically I'm running the same problem as this post http://stackoverflow.com/questions/2669672/accessing-mapped-drives-when-impersonating-in-asp-net

I'm working on a legacy website and I need to allow the admins to change the site's logo, banners, etc, from an image file on their desktops to a mapped drive on the server.

So, their website is using impersonation whenever it needs to save on the drive, and it's working just fine; however I can't manage to make it work on their test environment nor in my test environment.

¿Any ideas? I've double checked user and password (the code doesn't specify domain) and that's not the issue.

Here's an excerpt from the code that handles impersonation:

public bool ImpersonateUser(String user, String password, String domain)
{
    WindowsIdentity tempWindowsIdentity;
    IntPtr token = IntPtr.Zero;
    IntPtr tokenDuplicate = IntPtr.Zero;

    if (RevertToSelf())
    {
        if (LogonUserA(user, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
        {
            if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
            {
                tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                impersonationContext = tempWindowsIdentity.Impersonate();
                if (impersonationContext != null)
                {
                    CloseHandle(token);
                    CloseHandle(tokenDuplicate);
                    return true;
                }
            }
        }
    }
    //... rest of the code

And a -sanitized- test:

if (impUtility.ImpersonateUser("user", "password", string.Empty))
{
    fu.SaveAs(@"C:\Images\" + imgName);
}
A: 

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.

Ed B
@ Ed B: Thanks, I'll try it out. I have a couple of questions though: 1) ¿Does this solution imply that all the admins need to log in with the same account?2) I didn't get why you'd have to create mapped drive for each user; in my example, the admins would login with their accounts, but when about to write on the disk, the username and password used are from a privileged account (retrieved from the Web.config)
Gastón
1. No they don't. If the user is already logged into your site with Windows credentials, you can impersonate the logged-in user's Identity on the target machine like i'm doing above. 2. Ok, i didn't realize that there was a privileged account. I couldn't even get mapped drives method to work with my account already setup with mapped drives.
Ed B