PeerIdentityCreate and Impersonation

Hi, I want to create a set of unit tests for a library I have written on top of the Vista P2P apis. I want to use Impersonation in my tests to create identities for various users on my workstation. Unfortunately, when calling PeerIdentityCreate from an impersonated context, I receive the following error:

The system cannot find the file specified. (Exception from HRESULT 0x80070002)

(Code is .NET based, I am using Marshal.ThrowExceptionforHR on the HRESULT returned by PeerIdentityCreate)

The impersonation works fine (I am not loading the user profile currently), PeerIdentityCreate works fine when I am not impersonating. The User I am impersonating has the same set of rights as my development user (non admin), I have logged the user on before (windows already created a user profile for him).

I am wondering whether I have to load the userprofile when impersonating? If so, how do I do that? (LoadUserProfile requires some privileges and the calling user to be an admin)? When I log on the user I am impersonating under using Fast User switching parallel to my session, the tests work fine (I don't like this solution).

Any suggestions? What set of preconditions does PeerIdentityCreate require?

Thanks, Daniel

[1235 byte] By [Anonymoussgml';sdfgml;sdfgmsd;f] at [2008-2-7]
# 1

Hi Daniel,

I've reproduced your issue and yes, your hunch was correct - the user profile has to be loaded in order for the identity to be created when impersonating. The reason for this is that currently CreateIdentity leverages the win32 CryptAquireContext api, and that's just how that api works. This is from CryptAquireContext's msdn page:

Code Snippet
ERROR_FILE_NOT_FOUND ( 2L ): The profile of the user is not loaded and cannot be found. This happens when the application impersonates a user, for example, the IUSR_ComputerName account

So it looks like you'll have to use LoadUserProfile to get your scenario working as expected. Let me know if you have any further questions.

Thanks,

Vladimir

VladimirBobov-MSFT at 2007-10-3 > top of Msdn Tech,Windows Networking Development,Peer-to-Peer Networking...