簡體   English   中英

GetTokenSilentlyAsync不會執行

[英]GetTokenSilentlyAsync doesn't execute

我有一種奇怪的問題。 所以我試圖在Windows Universal App中連接到Office365。 我在互聯網上找到了Code,這很有效。 我在測試項目(在Visual Studio 2015中)中嘗試過它並且它有效。 所以我將它復制到項目中,我需要它。這是代碼:

public async void ConnectOffice()
    {
        string accessToken = await GetAccessTokenForResource("https://api.office.com/discovery/");
        DiscoveryClient discoveryClient = new DiscoveryClient(() =>
        {
            return accessToken;
        });

        CapabilityDiscoveryResult result = await discoveryClient.DiscoverCapabilityAsync("RootSite");
        var sharePointAccessToken = await GetAccessTokenForResource(result.ServiceResourceId);
        var sharePointServiceEndpointUri = result.ServiceEndpointUri.ToString();
        // Change global variable
        o365serverstring = result.ServiceResourceId;
    }


    public async Task<string> GetAccessTokenForResource(string resource)
    {
        string clientId = App.Current.Resources["ida:ClientId"].ToString();

        string token = null;

        //first try to get the token silently
        WebAccountProvider aadAccountProvider = await WebAuthenticationCoreManager.FindAccountProviderAsync("https://login.windows.net");
        WebTokenRequest webTokenRequest = new WebTokenRequest(aadAccountProvider, String.Empty, clientId, WebTokenRequestPromptType.Default);
        webTokenRequest.Properties.Add("authority", "https://login.windows.net");
        webTokenRequest.Properties.Add("resource", resource);
        WebTokenRequestResult webTokenRequestResult = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(webTokenRequest);
        if (webTokenRequestResult.ResponseStatus == WebTokenRequestStatus.Success)
        {
            WebTokenResponse webTokenResponse = webTokenRequestResult.ResponseData[0];
            token = webTokenResponse.Token;
        }
        else if (webTokenRequestResult.ResponseStatus == WebTokenRequestStatus.UserInteractionRequired)
        {
            //get token through prompt
            webTokenRequest = new WebTokenRequest(aadAccountProvider, String.Empty, clientId, WebTokenRequestPromptType.ForceAuthentication);
            webTokenRequest.Properties.Add("authority", "https://login.windows.net");
            webTokenRequest.Properties.Add("resource", resource);
            webTokenRequestResult = await WebAuthenticationCoreManager.RequestTokenAsync(webTokenRequest);
            if (webTokenRequestResult.ResponseStatus == WebTokenRequestStatus.Success)
            {
                WebTokenResponse webTokenResponse = webTokenRequestResult.ResponseData[0];
                token = webTokenResponse.Token;
            }
        }

        return token;
    }

調試它停在該行

WebTokenRequestResult webTokenRequestResult = await WebAuthenticationCoreManager.GetTokenSilentlyAsync(webTokenRequest);

並且不再繼續調試。

就像我說的。 我有一個項目與所示的完全相同的代碼,它工作得很好。 有任何想法嗎?

我認為你的問題是異步調試行為,而不是有問題的API。 https://msdn.microsoft.com/en-us/library/jj155813.aspx可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM