public static bool CheckPermissions(string userAndDomain, string pass, string path, FileSystemRights rights)
{
bool result;
// handle UNC path
try
{
string user = ExtractUserName(userAndDomain);
string domain = ExtractDomain(userAndDomain);
SafeTokenHandle safeTokenHandle;
bool loginOk = LogonUser(user, domain, pass, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
out safeTokenHandle);
if (loginOk)
{
using (safeTokenHandle)
{
var newID = new WindowsIdentity(safeTokenHandle.DangerousGetHandle());
using (WindowsImpersonationContext impersonatedUser = newID.Impersonate())
{
// Do the operation here
result = CheckPermissions(newID, path, rights);
impersonatedUser.Undo(); // remove impersonation now
}
}
}
else
{
// login failed
throw new Exception("Failed to authenticate with user [ " + userAndDomain + " ] for resource [ " +
path + " ] ");
}
}
catch (Exception ex)
{
Dev2Logger.Log.Error("FileIO", ex);
throw;
}
return result;
}