我在Xamarin.Forms应用中使用 Microsoft.Azure.Mobile.Client ,因为我需要在我的应用中进行离线同步。在azure中,我在App Service中配置了easy表,我需要只有经过身份验证的用户才能修改数据,因此我更改了表的权限,并为权限设置的所有选项设置了 Authenticated access only
在AAD上注册的申请
应用服务认证/授权
在应用程序中
我登录用户并使用此代码获取令牌。
string authority = "https://login.microsoftonline.com/common";
string resource = "https://graph.windows.net";
string clientId = "aca9a545-XXXXXXXXXX";
string returnUrl = "https://appservice.azurewebsites.net/.auth/login/aad/callback";
AuthenticationContext ac = new AuthenticationContext(authority);
AuthenticationResult authResult = await ac.AcquireTokenAsync(resource, clientId, new Uri(returnUrl), platformParameters);
此代码很好,我获得了令牌值(它打开了一个用户输入凭据并登录的窗口)。
MobileServiceClient Client = new MobileServiceClient("https://appservice.azurewebsites.net");
string path = Path.Combine(MobileServiceClient.DefaultDatabasePath, "DATA_BASE_NAME");
MobileServiceSQLiteStore Store = new MobileServiceSQLiteStore(path);
Store.DefineTable<Turn>();
Client.SyncContext.InitializeAsync(Store, new MobileServiceSyncHandler());
IMobileServiceSyncTable<Turn> Table = Client.GetSyncTable<Turn>();
var token = new JObject
{
{ "access_token", authResult.AccessToken}
};
var res = await Client.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, token); // First thread: here throw exception
await Client.SyncContext.PushAsync(); // Second thread: if I skip previous line, here throw exception too
await Table.PullAsync($"allTurns", Table.CreateQuery());
此代码是抛出异常的地方:
第一个线程异常:无法完成请求。 (未授权)
请求方法:POST,RequestUri:&#39; https://appservice.azurewebsites.net/.auth/login/aad&#39;,版本:2.0,内容:System.Net.Http.StringContent,标题: { X-ZUMO-INSTALLATION-ID:e8d8a571-fa9b-4ee8-905b-ca911c3d7d99 接受:application / json 用户代理:ZUMO / 3.1 User-Agent:(lang = Managed; os = Windows Store; os_version = - ; arch = X86; version = 3.1.50105.0) X-ZUMO-VERSION:ZUMO / 3.1(lang =托管; os = Windows应用商店; os_version = - ; arch = X86;版本= 3.1.50105.0) Accept-Encoding:gzip Content-Type:application / json;字符集= utf-8的 内容长度:1736 }
响应 StatusCode:401,ReasonPhrase:&#39; Unauthorized&#39;,版本:1.1,内容:System.Net.Http.StreamContent,标题: { Set-Cookie:ARRAffinity = 3911b1a0a4e4b012ff96f14ba9eb0231188f4dbe20b460dfa5c4e0166d608ed2; Path = /; HttpOnly; Domain = ghc-devtest-appservice.azurewebsites.net 日期:2017年11月29日星期三16:19:31 GMT WWW-Authenticate:Bearer realm =&#34; appservice.azurewebsites.net&#34; X-Powered-By:ASP.NET 内容长度:242 Content-Type:application / json }
第二个线程异常: Microsoft.WindowsAzure.MobileServices.Sync.MobileServicePushFailedException:推送操作失败。有关详细信息,请参阅PushResult。
PushResult.Status :Microsoft.WindowsAzure.MobileServices.Sync.MobileServicePushStatus.CancelledByAuthenticationError
PushResult.Errors :空
答案 0 :(得分:1)
方法:POST,RequestUri:&#39; https://appservice.azurewebsites.net/.auth/login/aad&#39;,版本:2.0,内容:System.Net.Http.StringContent,标题:{X-ZUMO-INSTALLATION-ID:e8d8a571- fa9b-4ee8-905b-ca911c3d7d99接受:application / json User-Agent:ZUMO / 3.1 User-Agent:(lang = Managed; os = Windows Store; os_version = - ; arch = X86; version = 3.1.50105.0)X- ZUMO-VERSION:ZUMO / 3.1(lang = Managed; os = Windows Store; os_version = - ; arch = X86; version = 3.1.50105.0)Accept-Encoding:gzip Content-Type:application / json; charset = utf-8内容长度:1736}
根据Authenticate users with the Active Directory Authentication Library,如果您尚未配置允许令牌受众,则需要将resource
替换为移动应用后端的客户端ID。
您在代码中创建了https://graph.windows.net
资源。因此,您需要在屏幕截图中的红色矩形位置添加https://graph.windows.net
,如下所示。
您还可以使用this对访问令牌进行解码,并验证其是否包含https://graph.windows.net
访问权限。