Xamarin Native 中的 AcquireTokenAsync 和 LoginAsync 之间的差异



TL;博士

使用AuthenticationContext.AcquireTokenAsync()MobileServiceClient.LoginAsync()对用户进行身份验证有什么区别?
我可以使用第一种方法中的令牌在第二种方法中对用户进行身份验证吗?


长版本

我一直在尝试通过移动设备(iOS) 对Azure中使用Xamarin 本机(不是窗体)的移动服务的用户进行身份验证。

网上有足够的教程来帮助你入门,但在这个过程中,我迷路了,很困惑......

目前有效的是以下内容; 它让用户在另一个页面中输入他的凭据并返回一个 JWT 令牌(如果在此处解码1)具有此处列出的声明2.

此外,此令牌在具有Authorization标头和Bearer token的请求中具有[Authorize]属性的控制器中授权。

注意:以下常量取自Active Directory(本机Web App/API)中已注册的应用程序。

public const string Authority = @"https://login.windows.net/******.com";
public const string GraphResource = @"https://*******.azurewebsites.net/********";
public const string ClientId = "046b****-****-****-****-********0290";
public const string Resource = @"https://******.azurewebsites.net/.auth/login/done";
var authContext = new AuthenticationContext(Authority);
if (authContext.TokenCache.ReadItems().Any(c => c.Authority == Authority))
{
authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
}
var uri = new Uri(Resource);
var platformParams = new PlatformParameters(UIApplication.SharedApplication.KeyWindow.RootViewController);
AuthenticationResult authResult = await authContext.AcquireTokenAsync(GraphResource, ClientId, uri, platformParams);

我尝试的另一个工作身份验证流程如下;它的作用相同,但不同之处在于它通知用户应用程序需要访问某些资源的权限。

如果允许,将返回 JWT 令牌(字符数少于前一个令牌),有效负载数据较少。但是,此令牌不会像上一个令牌一样传递授权属性。

public const string AadResource = @"https://******.azurewebsites.net/.auth/aad";
var client = new MobileServiceClient(AadResource);
var rootView = UIApplication.SharedApplication.KeyWindow.RootViewController;
MobileServiceUser user = await client.LoginAsync(rootView, "aad");

显然,返回类型是不同的,但是,这两种身份验证方法之间的主要区别是什么

此外,另一个令人头疼的是试图在文章的最后实现这3个。我已经拥有上述第一种方法的令牌,但是当我尝试使用令牌跟踪客户端流时client.LoginAsync()返回以下错误:

您要查找的资源已被删除、名称已更改或暂时不可用。


链接参考:

  1. https://jwt.io/
  2. https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-token-and-claims
  3. https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/enterprise/
  4. https://www.reddit.com/r/xamarindevelopers/comments/6dw928/differences_between_acquiretokenasync/

编辑(2017年5月30日)

Why are they different?

已经由同一个人(pdx mobilist/saltydogdev)在这4个reddit帖子上回答,简单的答案是claims

是的。您可以将令牌插入到移动服务客户端中,然后直接使用它已经过身份验证。这就是不记名代币的美妙之处。

只需设置 MobileServiceClient CurrentUser:

MobileServiceclient Client;
...
Client.CurrentUser = new MobileServiceUser(username) 
{ MobileServiceAuthenticationToken = authtoken};

编辑:

它们不同的原因是每个库请求一组不同的声明。它们仍然有效的原因是,用于验证/验证令牌的基本信息在那里。我不确定具体的要求是什么。至少是用户 ID 并且签名有效。他们正在做同样的基本事情,MobileServiceClient只是要求更少的索赔。

我相信,如果正确设置移动服务,移动服务客户端可以针对 Azure AD 进行身份验证。所以你应该能够只使用移动服务客户端。

以下是描述其工作原理的文档:https://learn.microsoft.com/en-us/azure/app-service-mobile/app-service-mobile-how-to-configure-active-directory-authentication

最新更新