At the moment I'm working with a WCF service that is used by an Asp.net MVC application.
For security reasons I'm using a guid that represents a username and pasword. ( When the user logs in, WCF checks the credentials in active directory and creates a record in a databasetable that connects the guid with the username and pasword) When the user uses another service, I send this guid along in the header. I want to impersonate the user in wcf using this guid. I've tried the folowing (using authorizationManager) but this doesn't work.
public class MyAuthBehaviour :ServiceAuthorizationManager
{
public override bool CheckAccess(OperationContext operationContext, ref Message message)
{
var index = operationContext.IncomingMessageHeaders.FindHeader("identifier", "");
if (index >= 0)
{
string identifier = operationContext.IncomingMessageHeaders.GetHeader<string>(index);
AddWindowsID(identifier);
}
return true;
}
private void AddWindowsID(string identifier)
{
WindowsIdentity wid = AccountBL.GetWindowsIdentity(identifier);
wid.Impersonate();
}
}
I do get the WindowsIdentity but I can't Impersonate. Is there a way to do this?
To say it short: I want to impersonate a user within WCF before it gets to the actual service method using the guid in the header.
When wid.Impersonate() hits, the client throws this exception:
The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.
WCF keeps on running.(although other exceptions where trown in the WCF services)
The exception isn't thrown when wid.Impersonate() is executed, it occurs when we're leaving the MyAuthBehaviour class