来自工作场所的无声认证在天蓝广告中加入机器

时间:2016-12-30 11:27:00

标签: c# azure asp.net-web-api azure-active-directory

我正在尝试在Azure广告中实现静默或无人参与的身份验证。我在Windows 10上安装了一个平板电脑,其中有工作区加入并且用户已登录。 我的平板电脑加入了工作场所" demo.onmicrosoft.com" &安培;登录用户是amit@demo.onmicrosoft.com。

直到这里一切都很好。现在问题在我的应用程序中。我有一个使用上述AAD从webapi获取数据的应用程序。 WebApi /数据库太过托管Azure但在其他一些订阅上。请注意,图片中有两个不同的Azure子目录。

如果我允许交互式登录(使用Azure身份验证弹出窗口),它的工作绝对正常。但是如果尝试抑制该窗口,它将返回一个空授权上下文。 以下是我的代码。

        /// <summary>
        /// Authenticate a user against azure ad
        /// </summary>
        /// <returns>true if authenticated otherwise false</returns>
        public bool Authenticate()
        {
            var success = false;
            AuthenticationResult result = null;
            // first, try to get a token silently
            try
            {
                result = _authContext.AcquireTokenSilentAsync(ResourceId, ClientId).Result;
            }
            catch (AggregateException exc)
            {
                AdalException ex = exc.InnerException as AdalException;
                // There is no token in the cache; prompt the user to sign-in.
                if (ex != null && ex.ErrorCode != "failed_to_acquire_token_silently")
                {
                    // An unexpected error occurred.
                    success = false;
                }
                else
                {
                    //var uc = new UserPasswordCredential("amit@demo.onmicrosoft.com","*********");
                    // if you want to use Windows integrated auth.
                    var uc = new UserCredential();
                    try
                    {
                        result = _authContext.AcquireTokenAsync(ResourceId, ClientId,uc).Result;
                    }
                    catch 
                    {
                        //some unexpected error, set auth failed
                        success = false;
                    }

                }
            }
            if (result != null)
            {
                AccessToken = result.AccessToken;
                success = AccessToken.Length > 0;
            }
            return success;
        }

0 个答案:

没有答案