简体   繁体   English

使用 Azure EasyTables 进行身份验证 - Azure ActiveDirectory

[英]Authenticating with Azure EasyTables - Azure ActiveDirectory

I've been attempting to authenticate requests to an Azure App Service for some time now and I'm completely stumped, I just can't seem to get the Microsoft.Azure.Mobile.Client to accept and successfully authenticate against a known "good" token.一段时间以来,我一直在尝试对 Azure 应用服务的请求进行身份验证,但我完全被难住了,我似乎无法让 Microsoft.Azure.Mobile.Client 接受并成功针对已知的“良好”进行身份验证"令牌。

Overall, all I want is to be able to successfully pull up a web browser in Xamarin Forms, authenticate the user either with Azure, Google or other social authentication, and then use a token to authenticate against an Azure App Service (EasyTables), which I already have running but without authentication enabled.总的来说,我想要的是能够在 Xamarin Forms 中成功启动 Web 浏览器,使用 Azure、Google 或其他社交身份验证对用户进行身份验证,然后使用令牌对 Azure 应用服务 (EasyTables) 进行身份验证,这我已经运行但没有启用身份验证。 For some reason none of the resources I've found have provided an clear way of doing this, and I'd be grateful for any help.出于某种原因,我发现的所有资源都没有提供明确的方法来做到这一点,我将不胜感激。

Here's the main config of what I have so far:这是我到目前为止的主要配置:

  1. I've got an app successfully reading and saving data tables to an Azure-hosting EasyTables implementation.我有一个应用程序成功读取数据表并将其保存到 Azure 托管 EasyTables 实现。 Tables are read (and written) using the standard form:使用标准形式读取(和写入)表:
var locations = (await App.MobileService.GetTable<Location>().ToListAsync());

The MobileServiceClient is instantiated in the App.xaml.cs file as follows: MobileServiceClient 在 App.xaml.cs 文件中实例化如下:

public static MobileServiceClient MobileService = new MobileServiceClient("https://mywebapp.azurewebsites.net");

As I say above, this works fine when access to and saving from online services.正如我上面所说,这在访问和保存在线服务时效果很好。

  1. Going to the Azure Portal, I've activated "App Service Authentication" under Settings --> Authentication / Authorization, and I've also set up an Azure Active Directory Authentication Provider.转到 Azure 门户,我在设置 --> 身份验证/授权下激活了“应用服务身份验证”,我还设置了 Azure Active Directory 身份验证提供程序。 Under this provider I've set up the Client ID of an Azure Active Directory instance (under Manage --> App Registrations).在此提供程序下,我设置了 Azure Active Directory 实例的客户端 ID(在管理 --> 应用程序注册下)。

  2. Going back to Xamarin, I have successfully managed to authenticate against this using the approach by Steven Thewissen here .让我们回到Xamarin,我已经成功地管理使用由史蒂芬Thewissen的办法对这种认证在这里 In particular, I've created an "MSAuthService" helper, which successfully pulls up a web browser, allows you to log in with Microsoft credentials, following which it's able to retrieve your account name and verious other things from Microsoft Graph - including the Access Token.特别是,我创建了一个“MSAuthService”帮助程序,它成功地启动了一个 Web 浏览器,允许您使用 Microsoft 凭据登录,然后它可以从 Microsoft Graph 检索您的帐户名和许多其他内容 - 包括 Access令牌。

  3. I'm now trying to use this access token to log into the MobileService I'm using to access EasyTables, using the following:我现在正在尝试使用此访问令牌登录到我用来访问 EasyTables 的 MobileService,使用以下命令:

                JObject auth_token_jobject = new JObject();
                auth_token_jobject["authenticationToken"] = token;

                var output = await App.MobileService.LoginAsync(
                    MobileServiceAuthenticationProvider.MicrosoftAccount,
                    auth_token_jobject);

However, whenever I do this, I still get an "Unauthorized" error, produced by the last line above.但是,每当我这样做时,我仍然会收到由上面最后一行产生的“未经授权”错误。

I understand that others (eg here seemed also to have the same problem, but no resolution on that post.我知道其他人(例如, 这里似乎也有同样的问题,但该帖子没有解决方案。

Other things that I've tried, but haven't managed to get working completely.我尝试过的其他事情,但还没有完全开始工作。 As above, the closest I've got, by successfully authenticating albeit through Microsoft Graph rather than with my web service specifically, is the process above:如上所述,通过 Microsoft Graph 而不是专门使用我的 Web 服务成功进行身份验证,我得到的最接近的过程是上述过程:

  1. Overview of Authorization with EasyTables etc here - although this doesn't seem to provide any clear code for Xamarin to authenticate against. EasyTables 等此处的授权概述 - 尽管这似乎没有为 Xamarin 提供任何清晰的代码来进行身份验证。
  2. Latest Xamarin blog and explanatory materials ( here and here , but although the process using await WebAuthenticator.AuthenticateAsync method appears to be a lot simpler than the example I was using above, there doesn't seem to be any detail provided about how you generate the URI required to call the authentication page, nor a step by step guide of how to implement it. Either way, I haven't managed to get it working...最新的 Xamarin 博客和说明材料( 此处此处,但尽管使用await WebAuthenticator.AuthenticateAsync方法的过程似乎比我上面使用的示例简单得多,但似乎没有提供有关如何生成调用身份验证页面所需的 URI,也没有关于如何实现它的分步指南。无论哪种方式,我都没有设法让它工作......

If anyone has an easy way of getting hold of a valid token and then providing it to the MobileService client, I'd be most grateful.如果有人可以轻松获取有效令牌,然后将其提供给 MobileService 客户端,我将不胜感激。 I suspect it's as simple of getting the token called back, for example from a Xamarin Essentials WebAuthenticator above, and then passing it with var output = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount,auth_token_jobject) but I just can't seem to get it working so far.我怀疑获取回调令牌很简单,例如从上面的 Xamarin Essentials WebAuthenticator,然后使用var output = await App.MobileService.LoginAsync(MobileServiceAuthenticationProvider.MicrosoftAccount,auth_token_jobject)传递它,但我似乎无法让它工作到目前为止。

Thanks a lot!非常感谢!

Oliver.奥利弗。

There are a couple of issues here (on re-reading it a few times)这里有几个问题(重新阅读几次)

  1. You are using MobileServiceAuthenticationProvider.MicrosoftAccount - you should be using "aad" instead.您正在使用MobileServiceAuthenticationProvider.MicrosoftAccount - 您应该使用“aad”。
  2. AAD needs an access token - see https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-how-to#validate-tokens-from-providers for the details on what needs to be provided. AAD 需要访问令牌 - 有关需要的详细信息,请参阅https://docs.microsoft.com/en-us/azure/app-service/app-service-authentication-how-to#validate-tokens-from-providers即将呈现。

If the token you get back is really an MSA token, then you still need to provide an access_token field (and not an authenticationToken field)如果您取回的令牌确实是 MSA 令牌,那么您仍然需要提供 access_token 字段(而不是 authenticationToken 字段)

If you don't need anything special, you should be able to just use .login() like this:如果你不需要任何特别的东西,你应该可以像这样使用 .login() :

await App.MobileService.LoginAsync("aad", "your-method");

For more details on this, see one of the authentica有关这方面的更多详细信息,请参阅其中之一

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM