C# CSOM Sharepoint Bearer request from azure active directory

后端 未结 1 1953
清歌不尽
清歌不尽 2021-01-19 06:25

I am using the following approach as the basis of this (https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-webapi-dotnet).

相关标签:
1条回答
  • 2021-01-19 07:05

    So this was possible, it was just microsoft telling us to put in an incorrect value. All the documentation says put the APP ID URI in the Resource. But in our case it needed to be the sharepoint url.

    So we have the tenant name which on azure id the domain name e.g. srmukdev.onmicrosoft.com

    Tenant: srmukdev.onmicrosoft.com

    Application Id: This is the guid for the app registered in azure active directory.

    RedirectUri: This can be any url(URI), its not actually used as a url for a mobile app as far as I can see.

    ResourceUrl: srmukdev.sharepoint.com

    The code I am using to get a token is as follows for a WPF example. The aadInstance is https://login.microsoftonline.com/{0}

    private static string authority = String.Format(CultureInfo.InvariantCulture, aadInstance, tenant);
    
    public async void CheckForCachedToken(PromptBehavior propmptBehavior)
        {
            //
            // As the application starts, try to get an access token without prompting the user.  If one exists, populate the To Do list.  If not, continue.
            //
            AuthenticationResult result = null;
            try
            {
                result = await authContext.AcquireTokenAsync(resourceUrl, applicationId, redirectUri, new PlatformParameters(propmptBehavior));
                TokenTextBox.Text = result.AccessToken;
                // A valid token is in the cache - get the To Do list.
                GetTokenButton.Content = "Clear Cache";
            }
            catch (AdalException ex)
            {
                if (ex.ErrorCode == "user_interaction_required")
                {
                    // There are no tokens in the cache.  Proceed without calling the To Do list service.
                }
                else
                {
                    // An unexpected error occurred.
                    string message = ex.Message;
                    if (ex.InnerException != null)
                    {
                        message += "Inner Exception : " + ex.InnerException.Message;
                    }
                    MessageBox.Show(message);
                }
                return;
            }
        }
    
    0 讨论(0)
提交回复
热议问题