我在Azure中的Web应用程序中获取我的API应用程序调用工作时遇到问题。这是事物的结构如何 -在Azure中调用web api时无法使用令牌令牌
- Asp.Net 1.1核心Web应用程序通过Azure的AD认证保护 - 利用红隼 的web应用程序的
StartUp.cs本地运行有以下代码用于获取令牌到Web API
app.UseCookieAuthentication();
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
ClientId = ClientId, //Client Id of my current web app
ClientSecret = ClientSecret, //ClientSecret of my current web app
Authority = "https://login.microsoftonline.com/tenantguid", CallbackPath = Configuration[Constants.ApplicationProxyCallbackPath],
ResponseType = OpenIdConnectResponseType.CodeIdToken,
Events = new OpenIdConnectEvents
{
OnAuthorizationCodeReceived = OnAuthorizationCodeReceived,
OnRemoteFailure = OnAuthenticationFailed
}
});
对于OnAuthorizationCodeReceived方法,这是我的代码
private async Task OnAuthorizationCodeReceived(AuthorizationCodeReceivedContext context)
{
string userObjectId = (context.Ticket.Principal.FindFirst(Constants.ClaimsSchemaUri))?.Value;
ClientCredential clientCred = new ClientCredential(ClientId, ClientSecret);
AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectId, context.HttpContext.Session));
AuthenticationResult authResult = await authContext.AcquireTokenByAuthorizationCodeAsync(
context.ProtocolMessage.Code,
new Uri(context.Properties.Items[OpenIdConnectDefaults.RedirectUriForCodePropertiesKey]),
clientCred,
WebAPIClientId);
}
使用上面的代码,我可以成功获取持票人令牌。
Controller类其中I做出的WebAPI
Task<string> results = null; string resultSet = String.Empty; AuthenticationResult authResult = null; string userObjectID = (currentUser.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value; AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID, current.Session)); ClientCredential credential = new ClientCredential(Startup.ClientId, Startup.ClientSecret); authResult = await authContext.AcquireTokenSilentAsync(Startup.SearchAPIClientId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId)); //var callerIdentity = currentUser.Identity as WindowsIdentity; HttpClientHandler handler = null; //Setup async action Action action =() => { handler = new HttpClientHandler() { AllowAutoRedirect = true }; //Setup for windows authentication var client = new HttpClient(handler); //Add common http headers client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"); client.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate"); client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.8"); client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authResult.AccessToken); results = client.GetStringAsync("https://myapi.azurewebsites.net/api/search/"); }; action.Invoke(); resultSet = await results as string;
- 在Web API与使用天青身份验证OpenIdConnect包中的代码,就好像Web应用程序代码以上保护。
的呼叫的呼叫到所述API被重新定向到login.microsftonline.com这意味着我的令牌不被理解。
我看了几个相关的帖子,但没有任何工作正常。
更新1 - 更新的Web API使用JWTBearer认证 现在我在网络应用程序中获取承载令牌是能够成功地验证了我对Web API。
我的Web API预计会调用另一个也受Azure AD身份验证保护的自定义API。我正在寻找相同的令牌,但为了启动我在使用获取令牌获取附加自定义API时遇到问题。它抛出内部服务器500没有消息。有什么想法吗?
更新2 - 详细的错误 在试图获取第三API令牌,我得到以下异常 - “AADSTS50105:应用程序源客户端ID GUID'未分配给应用程序一个角色“目标客户端id guid'。“
你是否能够在给授权标头赋予持有者令牌后从邮递员访问该API? – Venky
不,我不能。使用上面代码中生成的令牌,我无法使用邮递员进行呼叫。 –
你得到401或任何其他错误信息?如果令牌有效,那么您应该可以通过邮递员拨打电话。 – Venky