首页 文章

MSAL,Azure MobileService和Auto REST调用获得401 Unauthorized

提问于
浏览
-1

我有一个应用程序(目前在UWP中),它将MobileServiceClient和AutoRest用于Azure App Service API应用程序 . 我成功使用了winfbsdk并可以通过它进行身份验证,然后使用FB访问令牌作为JObject登录到MobileService.LoginAsync . 当我在应用程序中通过AutoRest调用API应用程序时,我也会使用该JObject并将其发送到x-zumo-auth标头中 . 我想要做的是能够使用MicrosoftAccount进行身份验证 . 如果我使用MobileService.LoginAsync,我无法获得正确的令牌并将其传递给AutoRest - 它总是以401 Unauthorized的形式返回 . 我试图使用MSAL,但它返回一个Bearer令牌,并且传递它也会返回401 Unauthorized . 有什么好办法吗?我从MSAL的路线开始,因为这将支持Windows桌面,UWP和Xamarin Forms,这将是理想的 . 我只需要有关如何从中获取正确令牌的信息传递给返回Azure App Service API应用程序的AutoRest HttpClient .

更新:如果我使用以下流程,它适用于Facebook,但 not with MicrosoftAccount.

-Azure AppService with WebAPI(并通过浏览器进行测试)

  • 通过服务上的Azure仪表板进行安全设置,并配置为允许Facebook或MicrosoftAccount

1.在我的UWP应用程序上,使用winfbsdk,我使用Facebook登录,然后获取FBSession.AccessTokenData.AccessToken并将其插入到JObject中:

JObject token = JObject.FromObject
  (new{access_token = fbSession.AccessTokenData.AccessToken});

2.登录MobileServiceClient

user = await App.MobileService.LoginAsync
  (MobileServiceAuthenticationProvider.Facebook, token);
  • 使用HttpClient登录API App并检索要在X-ZUMO-AUTH中使用的令牌
using (var client = new HttpClient())
{
    client.BaseAddress = App.MobileService.MobileAppUri; 
        var jsonToPost = token;

        var contentToPost = new StringContent(
         JsonConvert.SerializeObject(jsonToPost),
         Encoding.UTF8, "application/json");
        var asyncResult = await client.PostAsync(
        "/.auth/login/" + provider.ToString(),
        contentToPost);

        if (asyncResult.Content == null)
        {
            throw new InvalidOperationException("Result from call was null.");
            return false;
        }
        else
        {
            if (asyncResult.StatusCode == System.Net.HttpStatusCode.OK)
            {
                var resultContentAsString = asyncResult.Content.AsString();

                var converter = new ExpandoObjectConverter();
                dynamic responseContentAsObject = JsonConvert.DeserializeObject<ExpandoObject>(
                resultContentAsString, converter);

                var applicationToken = responseContentAsObject.authenticationToken;

                ApiAppClient.UpdateXZUMOAUTHToken(applicationToken);
            }
        }
    }
  • ApiAppClient.UpdateXZUMOAUTH调用只执行以下操作:
if (this.HttpClient.DefaultRequestHeaders.Contains("x-zumo-auth") == true)
    {
        this.HttpClient.DefaultRequestHeaders.Remove("x-zumo-auth");
    }

    this.HttpClient.DefaultRequestHeaders.Add("x-zumo-auth", applicationToken);
  • 使用ApiAppClient(使用来自我的Azure AppService WebAPI的swagger json的AutoRest创建)的任何后续调用都包含x-zumo-auth标头并且经过适当的身份验证 .

The problem occurs when trying to use MicrosoftAccount. 我似乎无法从MSAL或LoginWithMicrosoftAsync获取在x-zumo-auth中使用的正确令牌 .

对于上面的#1,在尝试使用MicrosoftAccount时,我使用了MSAL,如下所示:

AuthenticationResult result = await MSAuthentication_AcquireToken();  
JObject token = JObject.FromObject(new{access_token = result.Token});

MSAuthentication_AcquireToken定义如下,使用Azure示例中建议的接口和类:https://github.com/Azure-Samples/active-directory-xamarin-native-v2

private async Task<AuthenticationResult> MSAuthentication_AcquireToken()
       {
       IMSAcquireToken at = new MSAcquireToken();
        try
        {
            AuthenticationResult res;
            res = await at.AcquireTokenAsync(App.MsalPublicClient, App.Scopes);

            return res;
        }
      }

Update - ok with MobileServiceClient, but still not working with MSAL
我使用MobileServiceClient,如下所示:
1.使用MobileService.LoginAsync
2.获取返回的User.MobileServiceAuthenticationToken
3.将X-ZUMO-AUTH标头设置为包含User.MobileServiceAuthenticationToken

user = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount);  
applicationToken = user.MobileServiceAuthenticationToken;  
ApiAppClient.UpdateAppAuthenticationToken(applicationToken);

MSAL still not working!
所以最初的问题仍然存在,我们需要将从MSAL返回的令牌的哪一部分传递给X-ZUMO-AUTH或其他一些标头,以便对Azure AppService WebAPI应用程序的调用进行身份验证?

1 回答

  • 0

    我有一个应用程序(目前在UWP中),它将MobileServiceClient和AutoRest用于Azure App Service API应用程序 . 我成功使用了winfbsdk并可以通过它进行身份验证,然后使用FB访问令牌作为JObject登录到MobileService.LoginAsync . 当我在应用程序中通过AutoRest调用API应用程序时,我也会使用该JObject并将其发送到x-zumo-auth标头中 .

    根据你的描述,我假设你正在使用Client-managed authentication . 您直接联系身份提供商,然后在登录期间使用您的移动后端提供令牌,然后您可以利用 MobileServiceClient.InvokeApiAsync 来调用您的API APP,这将在您调用 MobileServiceClient.LoginAsync(MobileServiceAuthenticationProvider.Facebook, token); 后添加 X-ZUMO-AUTH 标头,其值为 authenticationToken

    我想要做的是能够使用MicrosoftAccount进行身份验证 . 如果我使用MobileService.LoginAsync,我无法获得正确的令牌并将其传递给AutoRest - 它总是以401 Unauthorized的形式返回 . 我试图使用MSAL,但它返回一个Bearer令牌,并且传递它也会返回401 Unauthorized . 有什么好办法吗?

    AFAIK, for the client-flow authentication patterns (AAD, Facebook, Google)LoginAsynctoken 参数看起来像 {"access_token":"{the_access_token}"} .

    For the client-flow authentication (Microsoft Account) ,您可以利用 MobileServiceClient.LoginWithMicrosoftAccountAsync("{Live-SDK-session-authentication-token}") ,也可以将 LoginAsync 与值 {"access_token":"{the_access_token}"}{"authenticationToken":"{Live-SDK-session-authentication-token}"}token 参数一起使用 . 我已经使用MSA的 access_token 测试了 LoginAsync 并检索了记录的信息,如下所示:

    enter image description here

    总之,当您使用移动后端登录后检索 authentionToken 时,可以使用 authentionToken 为每个API APP请求添加 X-ZUMO-AUTH 标头 .

    enter image description here

    有关详细信息,请参阅此官方文档authentication works in App Service .

    UPDATE

    我已经检查了这个https://github.com/Azure-Samples/active-directory-xamarin-native-v2并使用fiddler在验证用户身份时捕获网络包并获取访问令牌 . 我发现MSAL正在对抗Microsoft Graph and REST并且当用户登录时,您只能检索 access_tokenid_token ,并且它们都不能用于移动后端的单点登录 .

    虽然有关使用MSA的Azure移动应用程序的客户端管理身份验证的官方代码示例正在使用Live SDK . 正如Live SDK REST API提到signing users,您可以获得 access tokenauthentication token ,用于单点登录方案 . 此外,我检查了Server-managed authentication,发现MSA的应用服务身份验证/授权也使用Live SDK REST API . 总之,您无法使用MSAL进行客户端管理的MSA身份验证,对于客户端管理的身份验证,您需要利用Live SDK检索 authentication_token 然后调用 MobileServiceClient.LoginWithMicrosoftAccountAsync("{Live-SDK-session-authentication-token}") 以从您的移动后端检索 authenticationToken . 或者您可以只为MSA利用服务器管理的身份验证 . 有关Live SDK的更多详细信息,请参阅LiveSDK .

相关问题