Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net Azure移动应用程序服务API已验证用户身份_.net_Azure_Authentication_Xamarin.forms_Azure Mobile Services - Fatal编程技术网

.net Azure移动应用程序服务API已验证用户身份

.net Azure移动应用程序服务API已验证用户身份,.net,azure,authentication,xamarin.forms,azure-mobile-services,.net,Azure,Authentication,Xamarin.forms,Azure Mobile Services,我有一个Xamarin表单应用程序,它使用AD B2C进行身份验证,然后在Azure移动应用程序服务中调用API。应用程序服务使用.NET创建,并从数据库返回特定于用户的数据。问题是,我在TableController服务中获得的用户ID与在自定义API控制器服务中获得的用户ID不同。这两个服务都是使用Authorize属性设置的 第一个调用是移动应用程序服务TableController api,该api使用以下代码确定用户ID: ((ClaimsPrincipal)User).FindFir

我有一个Xamarin表单应用程序,它使用AD B2C进行身份验证,然后在Azure移动应用程序服务中调用API。应用程序服务使用.NET创建,并从数据库返回特定于用户的数据。问题是,我在TableController服务中获得的用户ID与在自定义API控制器服务中获得的用户ID不同。这两个服务都是使用Authorize属性设置的

第一个调用是移动应用程序服务TableController api,该api使用以下代码确定用户ID:

((ClaimsPrincipal)User).FindFirst(ClaimTypes.NameIdentifier).Value

客户的主要步骤如下: 1.从AD B2C获取令牌:PublicClientApplication.AcquireTokenAsync() 2.使用令牌登录到移动应用程序服务:MobileServiceClient.LoginAsync(在有效负载中传递令牌) 3.调用移动应用程序服务TableController API:IMobileServiceTable.ToEnumerableAsync()

这看起来不错。在表控制器中,从NameIdentifier声明中检索用户ID的值。此时的值的格式始终为:“sid:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”(x是十六进制字符)

然后,客户端调用另一个服务,该服务是ApicController(具有MobileAppController属性)。该服务使用与上面相同的代码来获取用户ID(来自NameIdentifier声明)。但是检索到的用户ID与TableController检索到的不同

客户端代码基本如下:

    authResult = await B2CPublicClientApp.AcquireTokenAsync();
    string url = [endpoint for the custom api]
    HttpClient client = new HttpClient();
    HttpRequestMessage message = new HttpRequestMessage(HttpMethod.Get, url);
    message.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.IdToken);
    HttpResponseMessage response = await client.SendAsync(message);
    string responseString = await response.Content.ReadAsStringAsync();
对自定义API端点的调用已成功,并且已成功授权,但用户ID不同。用户ID的格式为: xxxxxxxx-xxxx-xxxx-xxxx-XXXXXXXXXX,其中x是十六进制字符


显然,我需要一个用户在每次调用任何应用服务器服务时都具有相同的用户ID。

看起来这是两种不同的方法。类似的问题有一个答案-@mikalai-谢谢。这正是我想要的。