Incorrect windows identity in Cassini-Dev when hos

2019-09-14 08:06发布

问题:

I am hosting CassiniDev 4.0 in my windows service running an MVC 3.0 configuration site for my service.

I have the web.config setup to use windows authentication. When I look at the HttpContext.User in the web site, it shows the identity that the service is running under, not the itentity of the user making the request. The User.AuthenticationType is NTLM, which is correct, BTW.

This seems pretty clearly to be a bug, but wanted to run it by the community to see if there is some configuration I am missing.

It seems like it might be a variation on this issue postedlast week:

SecurityIdentifiers in Cassini-dev's NTLM authentication

回答1:

This is definitely a bug in Cassini Dev. It looks like this method is returning the wrong token: Request.GetUserToken(). The code:

public override IntPtr GetUserToken()
{
    return _host.GetProcessToken();
}

And here _host.GetProcessToken() is a pointer to a security token belonging to the user who owns the Cassini process, it is not the token belonging to the user that's logged in. What needs to happen is the NtlmAuth object needs to pass the security token back to the Request object so that it can be returned when this method is called instead of the host's token. Not really sure what the best way to do this is but you can see in the NtlmAuth class, the security token is acquired here:

IntPtr phToken = IntPtr.Zero;
if (Interop.QuerySecurityContextToken(ref _securityContext, ref phToken) != 0)
{
     return false;
}

phToken is the security token but it needs to get back to the Request object and not call Interop.CloseHandle(phToken); later in that method, where it frees the token. Note that CloseHandle() needs to be called on the token eventually, otherwise a new one will be issued for every request made by a logged in user but unused ones will never get freed. One possible place to do this is in the Request object, which subclasses SimpleWorkerRequest and you can override the EndOfRequest method to call CloseHandle() on the security token.