ASP.NET MVC: how can IPrincipal be null?

I am running a website on IIS6 / Server 2003 that uses integrated Windows authentication on a local intranet. I can go 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; 
} 

Since the error is intermittent, it is obvious that Windows Auth works at some level, but after navigating the site in a few clicks I get a zero link error, that is, IPrincipal is null (I thought it should never be empty in ASP .NET).

The error only occurs on a newly built virtual machine. The code is great for other machines and, of course, when developing locally.

Does IIS request Windows Auth data for each request? What could cause such an intermittent problem? Any help or suggestions would be greatly appreciated.

+3
source share
2 answers

I think this happened in this case because in IIS6 we checked Windows Auth and Anonymous. This seemed to create a situation where IIS sometimes used Windows Auth and therefore created an IPrincipal, but in other cases would use Anonymous Auth, which would not create an IPrinciple. Of course, disabling the Anonymous Out option seems to have resolved the issue.

+2
source

, Identity WindowsIdentity, . IIdenty FormsIdentity, ClientFormsIdentity, GenericIdentity, PassportIdentity WindowsIdentity, ,


typeof(principal.Identity);

.

+2

Source: https://habr.com/ru/post/1785906/


All Articles