private AuthenticationResult AcquireToken(AdalConfiguration config, string userId = null)
{
AuthenticationResult result = null;
Exception ex = null;
var thread = new Thread(() =>
{
try
{
var context = CreateContext(config);
if (string.IsNullOrEmpty(userId))
{
ClearCookies();
result = context.AcquireToken(config.ResourceClientUri, config.ClientId,
config.ClientRedirectUri, PromptBehavior.Always, AdalConfiguration.EnableEbdMagicCookie);
}
else
{
result = context.AcquireToken(config.ResourceClientUri, config.ClientId,
config.ClientRedirectUri, userId, AdalConfiguration.EnableEbdMagicCookie);
}
}
catch (Exception threadEx)
{
ex = threadEx;
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Name = "AcquireTokenThread";
thread.Start();
thread.Join();
if (ex != null)
{
var adex = ex as ActiveDirectoryAuthenticationException;
if (adex != null)
{
if (adex.ErrorCode == ActiveDirectoryAuthenticationError.AuthenticationCanceled)
{
throw new AadAuthenticationCanceledException(adex.Message, adex);
}
}
throw new AadAuthenticationFailedException(GetExceptionMessage(ex), ex);
}
return result;
}