我创建了一个 Xamarin.Forms 项目和一个 Table API 项目。两者都是通过 Azure 门户中的快速启动菜单创建的。
我已在门户中配置了 Azure AD,并且可以通过我的 XForms 应用程序成功从 AD 检索令牌。但是,当我尝试使用 MobileServiceClient 中的 LoginAsync 方法登录 Table API 时,我收到了“您无权查看目录或页面。”
我一直在浏览以下指南,但没有运气。
如何配置应用服务应用程序以使用 Azure Active Directory 登录 https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-how-to-configure-active-directory-authentication/#comments
将身份验证添加到您的 Xamarin.Forms 应用程序 https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-xamarin-forms-get-started-users/
如何:使用身份验证 https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-dotnet-backend-how-to-use-server-sdk/#how-to-work-with-authentication
我也查看了以下问题,但没有找到解决方案。
Cordova AAD 服务器流身份验证在 Android 和 iOS 上挂起 https://stackoverflow.com/questions/38576301/cordova-aad-server-flow-authentication-hangs-on-android-and-ios
我在想我可能缺少 Table API 项目的一些特定身份验证?
这是我的代码和设置:
XFORMS 中的 PCL 项目
var ar = await authContext.AcquireTokenAsync(Constants.GraphResourceUri, Constants.ClientId, userCredintials);
JObject payload = new JObject();
payload["access_token"] = ar.AccessToken;
var client = new MobileServiceClient(Constants.ApplicationUrl);
var user = await client.LoginAsync(MobileServiceAuthenticationProvider.WindowsAzureActiveDirectory, payload);
Constants.ClientID 是 Native Client 应用程序的 ClientId,而不是 Web 服务器的 ClientId。如果我改变它,我会得到 404。
表 API 项目中的控制器示例
[Authorize]
public class StatisticController : ApiController
表 API StartUp.cs 配置
public void Configuration(IAppBuilder app)
{
app.UseAppServiceAuthentication(new AppServiceAuthenticationOptions());
ConfigureMobileApp(app);
}
MOBILE SERVICE AUTH SETUP
SINGLE SING-ON AD SETTINGS
NATIVE CLIENT APP SETTINGS
EDIT
I can duplicate the error through PostMan with the following setup:
我尝试包含一个带有 access_token 值的“X-ZUMO-AUTH”标头,但结果相同。还是没有得到许可。我还尝试排除 POST 请求中的每个标头,但没有进行任何更改。这是否意味着不允许来自我的移动应用程序或 Postmand 的 POST 请求?
如果我在浏览器中手动浏览到 mysite.azurewebsites.net/.auth/login/aad,那么我可以使用 AD 中的用户登录。因此,AD 似乎与服务正确通信,反之亦然。
FIXED IT
非常感谢马琴德森 https://stackoverflow.com/users/3769274/mattchenderson!正如他建议的那样,我应该将常量 GraphResourceUri 更改为我的服务的客户端 ID,而不是普通的图形 api。除了添加客户端的单个实例之外,我现在可以成功登录到我的服务。