在 Azure Active Directory 中成功登录后,身份验证未通过

本文关键字:身份验证 登录 Active Azure Directory 成功 | 更新日期: 2023-09-27 17:57:24

我有一个使用 ADAL 对用户进行身份验证的应用程序。按下登录按钮后,将显示登录页面,但输入正确的凭据后没有任何反应。我已经检查了身份验证所需的所有变量(公共权限、重定向 URI、客户端 ID),但它仍然出来

这是身份验证部分;

private async Task<bool> AuthenticateUsingADAL(IPlatformParameters parent)
{
    var success = false;
    try
    {
        AuthenticationContext authContext = new AuthenticationContext(CommonAuthority);
        if (authContext.TokenCache.ReadItems().Count() > 0)
            authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
        AuthResult = await authContext.AcquireTokenAsync(ResourceUri, ClientId, RedirectUri, parent);
        //i put a WriteLine here but nothing goes through after the AuthResult. I don't know why
        success = true;
    }
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine("Authentication failed " + ex.ToString());
    }
    return success;
}

登录活动:

public bool Login(Activity activity)
{
    bool result = false;
    if (String.IsNullOrEmpty(Token) || TokenExpired) result = AuthenticateUsingADAL(new PlatformParameters(activity)).Result;
    return result;
}

这是从这个开始的:

private void BtnLogin_Click(object sender, EventArgs e)
{
    bool success = false;
    Task loginTask = new Task(() =>
    {
        success = SessionsHelper.Login(this);
    });
    loginTask.ContinueWith(t =>
    {
        if (success) GoToNextActivity();
    }, TaskScheduler.FromCurrentSynchronizationContext());
    loginTask.Start();
}

在 Azure Active Directory 中成功登录后,身份验证未通过

在这里,您正在尝试从缓存中读取内容。像下面这样更改您的代码,

var authContext = new Microsoft.ADAL.AuthenticationContext(authority);  
 authContext.tokenCache.readItems().then(function (items) {
 if (items.length > 0) {
        authority = items[0].authority;
        authContext = new Microsoft.ADAL.AuthenticationContext(authority);
 }
 // Attempt to authorize user silently
 authContext.acquireTokenSilentAsync(resourceUri, clientId)
 .then(authCompletedCallback, function () {
    // We require user cridentials so triggers authentication dialog
    authContext.acquireTokenAsync(resourceUri, clientId, redirectUri)
    .then(authCompletedCallback, errorCallback);
  });
});