RSS 2.0
# Friday, July 07, 2006

Updated August 13, 2006 to reflect Sander's comment.

On my old blog (in Dutch) I commented that I thought the WindowsImpersonationContext was clumsy and I wrote a replacement that you can use as follows:

Console.WriteLine("Current user: " + WindowsIdentity.GetCurrent().Name);
WrapperImpersonationContext context = new WrapperImpersonationContext(domain, username, password);
context.Enter();
// Execute code under other uses context
Console.WriteLine("Current user: " + WindowsIdentity.GetCurrent().Name);
context.Leave();
Console.WriteLine("Current user: " + WindowsIdentity.GetCurrent().Name);

Recently a visitor noted that the code wasn't quite right (missng a few using statements, and I found some other small issues. The correct code (including namespace references) is below. It takes care of all that nasty calls into the Win32 API. The one thing you need to be aware of is that it requires permissions to call into a DLL (i.e. run unsafe code), which is why I added the attributes that indicate this. Unfortunately that renders this class useless in a hosted environment, unless you strong sign the assembly and pursuade the host to allow your assembly to run in Full or High trust. This however is a problem you will run into regardless your use of this class. As soon as you call LogonUser you need at least High trust. If this is something that should be possible under lower trust by default it's up to the folks in Redmond to add this functionality to .NET and handle it as such.

using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Security.Permissions;
using System.ComponentModel;

public class WrapperImpersonationContext
{
   [DllImport("advapi32.dll", SetLastError = true)]
   public static extern bool LogonUser(String lpszUsername, String lpszDomain,
   String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

   [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
   public extern static bool CloseHandle(IntPtr handle);

   private const int LOGON32_PROVIDER_DEFAULT = 0;
   private const int LOGON32_LOGON_INTERACTIVE = 2;

   private string m_Domain;
   private string m_Password;
   private string m_Username;
   private IntPtr m_Token;

   private WindowsImpersonationContext m_Context = null;

   protected bool IsInContext
   {
      get { return m_Context != null; }
   }

   public WrapperImpersonationContext(string domain, string username, string password)
   {
      m_Domain = domain;
      m_Username = username;
      m_Password = password;
   }

   [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
   public void Enter()
   {
      if (this.IsInContext) return;
      m_Token = new IntPtr(0);
      try
      {
         m_Token = IntPtr.Zero;
         bool logonSuccessfull = LogonUser(
            m_Username,
            m_Domain,
            m_Password,
            LOGON32_LOGON_INTERACTIVE,
            LOGON32_PROVIDER_DEFAULT,
            ref m_Token);
         if (logonSuccessfull == false)
         {
            int error = Marshal.GetLastWin32Error();
            throw new Win32Exception(error);
         }
         WindowsIdentity identity = new WindowsIdentity(m_Token);
         m_Context = identity.Impersonate();
      }
      catch (Exception exception)
      {
         // Catch exceptions here
      }
   }

   [PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
   public void Leave()
   {
       if (this.IsInContext == false) return;
       m_Context.Undo();

       if (m_Token != IntPtr.Zero) CloseHandle(m_Token);
       m_Context = null;
   }
}

 

Friday, July 07, 2006 9:18:44 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [3] -
.NET | Development | English

This is a great tool that helps you create a sound threat model of your application, leading to a more secure application. Do yourself a favor and download it.

Friday, July 07, 2006 9:22:50 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
Development | English

Eerder al berichtte ik over deze Threat Modeling tool in Eerste hulp bij Threat Modeling toen dit nog een release candidate was. Deze tool is er nu als final release en is hier te downloaden.

Friday, July 07, 2006 9:19:06 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
Development | Nederlands
# Monday, July 03, 2006
Monday, July 03, 2006 9:23:45 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
ASP.NET | Nederlands
# Friday, June 30, 2006

De Prescriptive Architecture Guidance Group (PAG) heeft een tool ontwikkeld waar me je kunt nagaan of je ASP.NET applicatie wel voldoet aan de best practices. Scott Guthrie laat er meer van zien in zijn blog post Free Patterns and Practices Guidance Explorer for ASP.NET and .NET en je kunt de tool downloaden van http://www.gotdotnet.com/codegallery/codegallery.aspx?id=bb9aecfe-56ba-4ca9-8127-44e551b90962. Samen met FxCop en de Threat Modelling tool kun je zo steeds beter in kaart brengen of je applicatie goed gebouwd is.

Friday, June 30, 2006 10:35:07 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
ASP.NET | Nederlands

Onlangs heb ik Office 2007 Beta op mijn werkmachine gezet. Waarom? Een aantal features (zoals contstant de woord-count in de taakbalk zichbaar) zijn erg handig nu ik een boek aan het schrijven ben. Nu dacht ik dat dit geen neveneffecten had, maar ik heb er inmiddels een gevonden: Desktop Search is weg. Nou ja, niet helemaal. Het is nog geinstalleerd, maar er is geen zoekvakje meer waar je iets kunt invullen (wat het geheel redelijk nutteloos maakt). De oorzaak is volgens een post van Chris Pratley dat Office 2007 Beta ook WDS 3.0 installeert zodat je ook de nieuwe documentformaten van Office 2007 kunt doorzoeken. En daar gaat het dus mis... WDS werkt wel in de applicaties afzonderlijk, dus in Outlook (waarvoor ik het 't meest gebruik) kan ik gewoon zoeken. Voor bestanden moet ik even  wachten tot er een fix is. Heb je dit echt nodig zul je dus Office 2007 moeten verwijderen en WDS moeten herinstalleren.

Friday, June 30, 2006 1:36:27 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
Nederlands | Windows
# Tuesday, June 27, 2006

Ted Neward legt in zijn blogpost The Vietnam of Computer Science uit welke problemen zich voordoen bij object-relational mapping (de overgang van de database naar objecten en weer terug). Het stuk is behoorlijk lang, dus neem er even de tijd voor.

Tuesday, June 27, 2006 1:26:17 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
Development | Nederlands
# Monday, June 26, 2006

Met behulp van Typed DataSets en TableAdapters kun je een volwaardige Data Access laag maken voor je applicatie, en kun je met behulp van de ObjectDataSource control de nodige data in je applicatie tonen en bewerken. Hoe je dit allemaal kunt doen (en nog veel meer) is te lezen in een serie tutorials geschreven door ASP.NET guru Scott Mitchell.

Monday, June 26, 2006 9:25:58 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
ASP.NET | Nederlands

Threat Modeling van een applicatie, het identificeren van mogelijke bedreigingen en het registreren van de genomen maatregelen, wordt steeds belangrijker bij het maken van veilige applicaties. Zonder Threat Modeling is het eigenlijk niet mogelijk om te weten of een applicatie veilig is of niet. Aangezien Threat Modeling best lastig is, heeft Microsoft een tool om dit te doen, Microsoft Threat Analysis & Modeling. Hiervan is momenteel versie 2.0 Release Candidate 1 te downloaden, en binnenkort zal de uiteindelijke versie verschijnen. Deze tool bevat een wizard om je applicatie mee te definieren, waaruit de mogelijke threats worden gegenereerd. Zonodig kun je op basis van de Attack Library meer zaken toevoegen aan je model. Doe er je voordeel mee!

Overigens staat deze tool nog los van Visual Studio, hoewel je wel work items kunt maken en exporteren naar VS. Het schijnt de bedoeling te zijn dat deze tool t.z.t. onderdeel wordt van Visual Studio.

Monday, June 26, 2006 1:46:30 PM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
Development | Nederlands

WinFS, het op de PDC 2003 aangekondigde relationele file systeem, is op sterven na dood. Oorspronkelijk zou deze technologie in Windows Vista komen en zou je makkelijk je bestanden kunnen organiseren. Op het WinFS blog van Microsoft is nu aangekondigd wat er gaat gebeuren met WinFS. In het kort: onderdelen van de technologie komen in de volgende versie van ADO.NET en SQL Server terecht, maar het zal niet meer geleverd worden als aparte technologie. Daarmee is de gedachte van het relationele file systeem min of meer van de baan zo lijkt het. De kritiek op dit bericht is niet van de lucht (zie comments bij het bericht), en terecht. Door deze koerswijziging is iets wat geweldig is voor zowel bedrijven als consumenten de nek opgedraaied en wordt het expliciet in de bedrijfssfeer neergezet.

Monday, June 26, 2006 10:26:46 AM (W. Europe Daylight Time, UTC+02:00)  #    Comments [0] -
Development | Nederlands
Sign In

Archive
<July 2006>
SunMonTueWedThuFriSat
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345
About
This is the blog of Michiel van Otegem, a Senior Software Architect with Sogeti Netherlands, and author of several books and numerous articles on (ASP).NET, XML, and related technologies.
Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Michiel van Otegem
All Content © 2012, Michiel van Otegem
DasBlog theme 'Business' created by Christoph De Baene (delarou)