Impersonation?



I realize this thread is over a year old, but I'll answer, anyway....

Sounds like you're trying to impersonate the user. This should get you started. Once you have the user impersonated, you can use CredentialCache.DefaultCredentials to do whatever you need the user to do (i.e. access a file via Stream, for instance).

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;
using System.Security.Principal;
using System.Runtime.InteropServices;
using WUApiLib;


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

[DllImport("advapi32.dll", EntryPoint = "LogonUser")]
public static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);

[DllImport("advapi32.dll", EntryPoint = "CloseHandle")]
public static extern bool CloseHandle(IntPtr hObject);

// Declare the logon types as constants
const int LOGON32_LOGON_INTERACTIVE = 2;
const int LOGON32_LOGON_NETWORK = 3;

// Declare the logon providers as constants
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_PROVIDER_WINNT50 = 3;
const int LOGON32_PROVIDER_WINNT40 = 2;
const int LOGON32_PROVIDER_WINNT35 = 1;

protected void btnSubmit_Click(object sender, EventArgs e)
{
IntPtr tokenHandle = new IntPtr(0);
tokenHandle = IntPtr.Zero;

// Get the domain and user name
string[] arUser = new string[1];
char[] splitter = { '\\' };
string userId = txtUserID.Text;
arUser = userId.Split(splitter);
string domainName = arUser[0];
string userName = arUser[1];
string password = txtPassword.Text;

// Call LogonUser to obtain a handle to an access token
bool returnValue = LogonUser(userName, domainName, password,
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_WINNT50, ref tokenHandle);

if (returnValue == false)
{
int ret = Marshal.GetLastWin32Error();
MessageBox.Show("LogonUser failed with error code : " + ret.ToString());
MessageBox.Show("\nError: " + ret.ToString());
}

MessageBox.Show("Did LogonUser Succeed? " + returnValue.ToString());
MessageBox.Show("Value of Windows NT token: " + tokenHandle.ToString());

if (returnValue == true)
{
//Impersonation
MessageBox.Show("Before impersonation: " +
WindowsIdentity.GetCurrent().Name);

WindowsIdentity newId = new WindowsIdentity(tokenHandle);
WindowsImpersonationContext impersonatedUser = newId.Impersonate();

MessageBox.Show("After impersonation: " +
WindowsIdentity.GetCurrent().Name);

// Do your magic here...

impersonatedUser.Undo();

// Free the token
if (tokenHandle != IntPtr.Zero)
CloseHandle(tokenHandle);
}

That should get you started...

-Tom

.



Relevant Pages

  • Re: Remote call to COM impersonating another user
    ... LOGON32_PROVIDER_DEFAULT, ref tokenHandle); ... string lpszUserName, ... int dwLogonType, ...
    (microsoft.public.dotnet.languages.csharp)
  • Verify a Windows Account
    ... static bool authUserLocal(string username, string password) ... public static extern bool LogonUser(string lpszUsername, ... int dwLogonType, int dwLogonProvider, out int phToken); ...
    (microsoft.public.dotnet.framework)
  • Verify a Windows Account
    ... static bool authUserLocal(string username, string password) ... public static extern bool LogonUser(string lpszUsername, ... int dwLogonType, int dwLogonProvider, out int phToken); ...
    (microsoft.public.dotnet.general)
  • RE: Access File Share from ASP.NET using Unmanaged Code
    ... Do you have impersonation enabled in your web.config file, and Windows authentication setup in IIS? ... "Mark Duregon" wrote: ... > public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword, ... > int dwLogonType, int dwLogonProvider, ref IntPtr TokenHandle); ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Big-Picture Question (Web Services, RegNow)
    ... I just tried returning a string array. ... there is no way to name each of the string fields returned. ... public ReturnValue GetRegistrationCode() ... But only seems like it would be harder for the consumer of my Web service to deal with. ...
    (microsoft.public.dotnet.framework.aspnet)

Quantcast