繁体   English   中英

成功登录Azure Active Directory后无法通过身份验证

[英]Authentication not going through after successful login in Azure Active Directory

我有一个使用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();
}

在这里,您尝试从缓存中读取内容。 如下更改代码,

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);
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM