Async Task <object>
public async Task<UserInfo> GetUserDataAsync(string NetworkID) { PrincipalContext principalcontext = new PrincipalContext(ContextType.Domain, ADDomain, ADUser, ADPass); UserPrincipal founduser = null; await Task.Run(() => { founduser = UserPrincipal.FindByIdentity(principalcontext, IdentityType.SamAccountName, NetworkID); }); //task.Wait(); return founduser != null && founduser.Enabled == true ? new UserInfo { DisplayName = founduser.DisplayName, Email = founduser.EmailAddress, NetworkID = founduser.SamAccountName } : new UserInfo(); }
This is a function that allows you to validate the user through the declaration, I can run the function with Task task = Task.Factory.StartNew (() => {}); and task.Wait (); but this will still work in sync. Executing the code above with async and waiting leaves the browser dead-locked, spinning forever and never returning a value.
+3
source share
2 answers
If you want to do this asynchronously, why not just return the task directly?
public Task<UserInfo> GetUserDataAsync(string NetworkID) { return Task.Run(() => { PrincipalContext principalcontext = new PrincipalContext(ContextType.Domain, ADDomain, ADUser, ADPass); UserPrincipal = UserPrincipal.FindByIdentity(principalcontext, IdentityType.SamAccountName, NetworkID); return founduser != null && founduser.Enabled == true ? new UserInfo { DisplayName = founduser.DisplayName, Email = founduser.EmailAddress, NetworkID = founduser.SamAccountName } : new UserInfo(); }); }
+1
source share