Azure移动服务和Azure Web App身份验证

时间:2016-03-25 23:51:09

标签: azure azure-web-sites azure-mobile-services azure-active-directory claims-based-identity

当用户通过Azure Web App(ASP.NET MVC)和Xamarin.iOS应用程序登录时,我为同一用户获得两个不同的SID

设置

带有API控制器的Azure WebApp ASP.NET 5

Xamarin iOS应用程序与Microsoft.WindowsAzure.Mobile.Client SDK Azure B2C AAD

网络上的用户

我得到的是Object Adentifier值,即AAD SID:

var userClaim = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;

移动用户

我只获取Nameidentifier值而没有ObjectIdentifier

ClaimsPrincipal mobileUser = this.User as ClaimsPrincipal;
var mobileUserClaim = mobileUser.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier");

SID完全不同,用户通过Mobile进行身份验证的SID获取SID:xxxx,而来自Web获取xxx

我知道如果我设置一个Azure移动应用程序和一个Azure Web应用程序,则在进行身份验证时SID是相同的。但我不想管理我的应用程序的大小(小)的两个网站。该应用程序的目的是让一个简单的Web应用程序在电话上执行某些操作和相同的操作,从电话我使用Azure移动服务SDK和InvokeAPIAsync来使用Web应用程序中的API控制器。

由于

2 个答案:

答案 0 :(得分:1)

我想澄清一下情况。 您正在观察两个SID:

1)从AAD,通过网络浏览器登录AAD。

2)来自Azure App Service(Web App和Mobile App),可能来自我们客户端的LoginAsync。此方法将调用服务器控制的登录流程。

这是设计的。 MobileServiceClient获取App Service令牌,并使用该令牌对您的Mobile App进行身份验证。您可以通过对.auth / me端点进行GET来交换从Azure App Service获取的用于AAD SID的身份验证令牌。

使用App Service和AAD对客户端进行身份验证后,您可以通过调用yoursite.azurewebsites.net/.auth/me获取有关AAD用户(或任何身份提供者)的更多信息,并解析声明的响应想要:

({“typ”:“http://schemas.microsoft.com/identity/claims/objectidentifier”)。

另一种策略是使用客户端应用程序中的ADAL(http://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/)登录AAD,然后使用AAD访问令牌使用相应的LoginAsync重载获取移动应用令牌:

https://github.com/Azure/azure-mobile-apps-net-client/blob/master/src/Microsoft.WindowsAzure.MobileServices.iOS/Extensions/MobileServiceClientExtensions.cs#L55

您添加的参数应采用格式{“access_token”:“[AAD access_token value]”}

Brett Samblanet在.NET Server上关于用户ID的wiki应该有助于了解发生了什么: https://github.com/Azure/azure-mobile-apps-net-server/wiki/Understanding-User-Ids

答案 1 :(得分:0)

我终于开始工作了:

string authority = "https://login.windows.net/[TentantId].onmicrosoft.com/";
            string resourceId = "[myApiClientId]";
            string clientId = "[clientId]";
            string redirectUri = "https://[URL]/.auth/login/done";

            AuthenticationContext ac = new AuthenticationContext(authority);
            AuthenticationResult ar = await ac.AcquireTokenAsync(resourceId, clientId,
                new Uri(redirectUri), new PlatformParameters(this));
            JObject payload = new JObject();
            payload["access_token"] = ar.AccessToken;

            string authHeader = ar.CreateAuthorizationHeader();
            HttpClient client = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "[API URL Route]");
            request.Headers.TryAddWithoutValidation("Authorization", authHeader);
            HttpResponseMessage response = await client.SendAsync(request);
            string content = await response.Content.ReadAsStringAsync();