Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindByIdentity - performance differences

The following code works fine from a variety of machines on our domain.

var context = new PrincipalContext(ContextType.Domain);
var principal = UserPrincipal.FindByIdentity(context, @"domain\username")

However, if I run this similar code on a machine that is not on a domain, it works but the FindByIdentity line takes 2+ seconds.

var context = new PrincipalContext(ContextType.Machine);
var principal = UserPrincipal.FindByIdentity(context, @"machinename\username")

Can this performance difference be addressed by supplying special parameters to the PrincipalContext constructor and/or the FindByIdentity method? Is there a setting in IIS or Windows that could be tweaked?

At the very least, can anybody tell me why it might be slower in the second scenario?

The code is running from an ASP.NET MVC 3 app hosted in IIS 7.5 (Integrated Pipeline) on Windows Server 2008 R2.

like image 205
Mayo Avatar asked Sep 23 '11 19:09

Mayo


2 Answers

I had the same problem. Try the below block of code. I don't know why but it's much faster (ignore first time slow login after build in VS - subsequent logins are speedy). See similar SO question Why would using PrincipalSearcher be faster than FindByIdentity()?

var context = new PrincipalContext( ContextType.Machine );
var user = new UserPrincipal(context);
user.SamAccountName = username;
var searcher = new PrincipalSearcher(user);
user = searcher.FindOne() as UserPrincipal;

The underlying issue may have something to do with netBios calls. See ADLDS very slow (roundtrip to \Server*\MAILSLOT\NET\NETLOGON)

like image 94
JimSTAT Avatar answered Nov 09 '22 03:11

JimSTAT


Even with JimSTAT's answer being quite old, it led me into the right direction solving my problem. I would like to point out that disabling NetBIOS over TCP/IP on all my Hyper-V-Switch network interfaces eliminated every delay I had experienced beforehand. Every call to PrincipalContext(), UserPrincipal.FindByIdentity() and UserPrincipal.GetGroups() is down to 10 ms or lower, from well above 10,000 ms. It appears to me that every virtual network card was being queried which took a felt century to time out. Lucky me I found this post!

like image 23
Klimi Avatar answered Nov 09 '22 02:11

Klimi