2
I implemented an authentication via AD in my application. It only works on the second try, it does not work on the first.
How do I resolve?
DirectoryEntry directoryEntry = new DirectoryEntry(path, domUser, password, AuthenticationTypes.Secure);
//Pesquisa na directoria
DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry);
try
{
//Procura credenciais e autentica utilizador
SearchResult searchResult = directorySearcher.FindOne();
//Procura utilizador por username e armazena em sessão o seu nome completo
using(var ctx = new PrincipalContext(ContextType.Domain))
{
using (UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username))
{
if (user != null)
{
string name = user.DisplayName;
//Armazena nome completo do utilizador
Session["name"] = name;
}
}
}
Session["LoggedIn"] = true;
directorySearcher.Dispose();
Response.Redirect("~/Home/Index");
}
catch
{
Response.Redirect("~/Home/Login");
}
PS: I deleted domain information from the code for privacy reasons of my employer
Have you tried debugging the code and see what happens the first time?
– Jéf Bueno
What doesn’t work the first time and only the second time? Where is the request for authentication and authorization?
– Leandro Angelo