- mullivan.sharepoint.zip
- Build
- Features
- AdUserInfoUpdate
- Reminder
- gacutil.exe
- Images
- ReminderSettings.gif
- Layouts
- Mullivan.Extensibility.dll
- Mullivan.Extensibility.dll.config
- Mullivan.Extensibility.pdb
- Mullivan.Shared.dll
- Mullivan.Shared.pdb
- Mullivan.SharePoint.dll
- Mullivan.SharePoint.Pages.dll
- Mullivan.SharePoint.Pages.pdb
- Mullivan.SharePoint.pdb
- Mullivan.SharePoint.Reminders.dll
- Mullivan.SharePoint.Reminders.pdb
- Mullivan.SharePoint.Remote.dll
- Mullivan.SharePoint.Remote.dll.config
- Mullivan.SharePoint.Remote.pdb
- Mullivan.SharePoint.WebParts.dll
- Mullivan.SharePoint.WebParts.pdb
- Update SharePoint.bat
- WebpartFiles
- LibraryNavigationWebPart.webpart
- ListQueryResultsWebPart.webpart
- ListQueryWebPart.webpart
- MostViewedWebPart.webpart
- WeatherWebPart.webpart
- Mullivan.Shared
- Collections
- Generic
- Cryptography
- Logging
- Mullivan.Shared.csproj
- Mullivan.Shared.csproj.user
- Mullivan.snk
- Properties
- Security
- Serialization
- Mullivan.SharePoint.Pages
- Mullivan.SharePoint.Reminders
- Mullivan.SharePoint.Remote
- Mullivan.SharePoint.sln
- Mullivan.SharePoint.WebParts
- Mullivan.SharePoint
|
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.ComponentModel;
namespace Mullivan.Security
{
public static class CredentialImpersonate
{
private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_PROVIDER_DEFAULT = 0;
public static WindowsImpersonationContext Impersonate(string domain, string userName, string password)
{
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
try
{
if (LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token))
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
return new WindowsIdentity(tokenDuplicate).Impersonate();
}
else
throw new Win32Exception(Marshal.GetLastWin32Error(), "DuplicateToken Failed");
}
else
throw new Win32Exception(Marshal.GetLastWin32Error(), "LogonUser Failed");
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
}
}
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(
String lpszUsername,
String lpszDomain,
String lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private static extern bool CloseHandle(IntPtr handle);
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.