我跟着this tutorial通过Azure AD对我的Azure移动服务进行身份验证,但我无法让URL重定向工作。也许我误会了,但这是我试过的......
在iOS项目中
我将此URL方案添加到info.plist:com.spike.Xamarin.OAuth:/oauth2redirect
我重写AppDelegate.OpenUrl
方法(两个重载)并实现教程中的代码
在PCL项目中
我将身份验证网址传递为https://(my service).azurewebsites.net/.auth/login/aad
,而重定向网址则将相同的网址(上方)传递给Xamarin.Auth.OAuth2Authenticator
ctor。
我向Xamarin.Auth.OAuth2Authenticator.Completed
事件添加了一个事件处理程序,以获取访问令牌等等。
在我受保护的Azure移动服务中(AAD / Express)
我将上述网址注册为OAuth.Xamarin.spike.com:/oauth2redirect
,作为“允许的外部重定位网址”。
结果
当我进入登录页面并且我能够按预期填写我的凭据时会发生什么。但最终的重定向永远不会遇到AppDelegate.OpenUrl方法。相反,iOS Safari似乎最终在https://(my service).azurewebsites.net/.auth/login/aad/callback
。
我在这里弄错了什么?
答案 0 :(得分:2)
Xamarin.Auth包含OAuth身份验证器,可为消费身份提供商(例如Google,Microsoft,Facebook和Twitter等)提供支持。
根据我的理解,您可以利用 Xamarin.Auth SDK独立联系身份提供商并在移动客户端检索访问令牌,然后您需要使用后端登录(天蓝色移动设备)应用)以及用于检索authenticationToken
的令牌,然后您可以利用authenticationToken
访问移动应用下的资源。
对于Azure AD身份验证,您可以参考以下设置来构建OAuth2Authenticator
,如下所示:
授权网址至https://login.microsoftonline.com/{tenantId}
将网址重定向到{Client-ID-of-your-AD-app}:/oauth2redirect
AccessToken Url 至https://login.microsoftonline.com/{tenantId}/oauth2/token
有关详细信息,请参阅OAuth 2.0 authorization flow for AAD。
从AAD检索访问令牌后,您需要使用移动应用程序发送以下请求进行记录:
Post: https://{your-app-name}.azurewebsites.net/.auth/login/aad
payload: {"access_token":"{your-access-token}"}
此外,您还可以Microsoft.Azure.Mobile.Client或Client-managed authentication使用Server-managed authentication。有关向应用添加身份验证的详细信息,可以参考Add authentication to the portable class library和Add authentication to the iOS app获取Xamarin Forms应用。