【问题标题】:C# CSOM Sharepoint Bearer request from azure active directory来自 Azure Active Directory 的 C# CSOM Sharepoint Bearer 请求
【发布时间】:2017-07-29 04:08:59
【问题描述】:

我使用以下方法作为此方法的基础 (https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-webapi-dotnet)。

在设置 azure 后,我得到了所有这些示例。但现在我们需要将其移植到实际现有的移动应用程序和 Web api 应用程序。移动应用可以获取 Bearer 令牌,但是当我们将其传递给 web api 时,我们将其传递到 CSOM 请求中,如下所示,但我们仍然得到一个 401 未经授权的响应。

public static ClientContext GetSharepointBearerClientContext(this JwtTokenDetails tokenDetails)
    {
        var context = new ClientContext(tokenDetails.SiteUrl);
        //context.AuthenticationMode = ClientAuthenticationMode.Anonymous;
        context.ExecutingWebRequest += new EventHandler<WebRequestEventArgs>((s, e) =>
        {
            e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + tokenDetails.BearerToken;
        });
        return context;
    }

我们的 web api 没有使用上面示例中的任何技术,因为我认为我们应该能够通过标头中的 CSOM 请求传递令牌,但这不起作用,还有什么可以我看?

我已分配 Office 365 Sharepoint Online (Microsoft.Sharepoint) 权限并设置以下内容

我也为应用注册做了同样的事情,我们并没有真正使用!仍然不确定应用注册是如何进入其中的)...

【问题讨论】:

    标签: sharepoint azure-active-directory csom


    【解决方案1】:

    所以这是可能的,只是微软告诉我们输入错误的值。所有文档都说将 APP ID URI 放在资源中。但在我们的例子中,它必须是共享点 url。

    所以我们有租户名称,它在 azure id 上是域名,例如srmukdev.onmicrosoft.com

    租户:srmukdev.onmicrosoft.com

    应用程序 ID:这是在 azure 活动目录中注册的应用程序的 guid。

    RedirectUri:这可以是任何 url(URI),据我所知,它实际上并未用作移动应用程序的 url。

    ResourceUrl:srmukdev.sharepoint.com

    我用来获取令牌的代码如下,用于 WPF 示例。 aadInstance 是 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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2021-10-22
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多