Question

I'm running a website on IIS6 / Server 2003 which uses Integrated Windows Authentication on a local intranet. I can browse to the site but get intermittent "Object null" errors when calling the following C# code, which is called on every request:

.... 
GetUserIdFromPrincipal(User) 
....  

public static string GetUserIdFromPrincipal(IPrincipal principal) {     
     return principal.Identity is WindowsIdentity ? (principal.Identity as WindowsIdentity).User.Value : principal.Identity.Name; 
} 

So as the error is intermittent clearly Windows Auth is working on some level but after navigating around the site for several clicks I get the null reference error meaning IPrincipal is null (I thought this should never be null in ASP.NET).

The error only happens on a newly built VM. The code is fine on other machines and certainly when developing locally.

Does IIS request the Windows Auth details on each request? What would cause such an intermittent problem? Any help or suggestions would be much appreciated.

Was it helpful?

Solution 2

I think in the case above it was because we had Windows Auth and Anonymous checked in IIS6. This seemed to create the situation that IIS would sometimes use Windows Auth and hence create an IPrincipal but other times would use Anonymous Auth which would not create an IPrinciple. Certainly turning off the Anonymous Auth option seems to have resolved the problem.

OTHER TIPS

I would suggest, that your Identity is simply not a WindowsIdentity and therefore the case will fail and leed to a null-object. IIdenty might be FormsIdentity, ClientFormsIdentity, GenericIdentity, PassportIdentity and WindowsIdentity, I guess you should try


typeof(principal.Identity);

to get the real one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top