Ein Problem mit Azure Auth. Abrufen 401 Nicht autorisiert beim Aufrufen von in Azure gehostetem WebAPI.Azure AD - HTTP/1.1 401 Nicht autorisiert - Windows-Dienst
Hier ist die Grundlagen: dieses Tutorial Gefolgt - https://azure.microsoft.com/en-us/documentation/samples/active-directory-dotnet-daemon/
Registrierte 2 Anwendungen:
- Windows Service
- WebAPI
Der Windows-Dienst ruft STS und Erfolgreich bekommt ein JWT . Es versucht dann, dieses Token an die WebAPI zu übergeben, die auf azurewebsites.net gehostet wird.
Hier ist mein Code die STS (Just in einer Auth Klasse verpackt) zu nennen:
public class AzureAuth
{
private AuthenticationContext authContext;
private ClientCredential clientCredential;
protected string AzureADInstanceName;
protected string AzureADTenancyName;
protected string AzureADApplicationClientId;
protected string AzureADApplicationSecret;
protected string AzureADApplicationAppId;
public AzureAuth(string azureADInstanceName, string azureADTenancyName, string azureADApplicationClientId, string azureADApplicationSecret, string azureADApplicationAppId)
{
this.AzureADInstanceName = azureADInstanceName;
this.AzureADTenancyName = azureADTenancyName;
this.AzureADApplicationClientId = azureADApplicationClientId;
this.AzureADApplicationSecret = azureADApplicationSecret;
this.AzureADApplicationAppId = azureADApplicationAppId;
string authority = string.Format(CultureInfo.InvariantCulture, AzureADInstanceName, AzureADTenancyName);
this.authContext = new AuthenticationContext(authority);
this.clientCredential = new ClientCredential(AzureADApplicationClientId, AzureADApplicationSecret);
}
public async Task ExecuteGetWithAADAutToken<T>(Action<AuthenticationResult> AfterAuthAction)
{
AuthenticationResult result = null;
try
{
result = await authContext.AcquireTokenAsync(this.AzureADApplicationAppId, this.clientCredential);
AfterAuthAction(result);
}
catch (Exception ex)
{
throw;
}
}
}
ich dann, dass Auth-Klasse und die AuthResult mit der WebAPI zu nennen:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(getEndPointUri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", res.AccessToken);
HttpResponseMessage response = await client.GetAsync("/api/field");
if (response.IsSuccessStatusCode)
{
var resultStringJSON = await response.Content.ReadAsStringAsync();
Console.WriteLine(resultStringJSON);
}
else
{
var resulterrorStringJSON = await response.Content.ReadAsStringAsync();
Console.WriteLine(resulterrorStringJSON);
}
}
die HttpResponseMessage Antwort geben Unerlaubte
I Fiedler verwendet, um die Anfrage und Antwort zu erhalten:
GET https://xxxxxxxx.azurewebsites.net/api/someapi HTTP/1.1
Accept: application/json
Authorization: Bearer XXXXXXXXX
Host: xxxxxx.azurewebsites.net
HTTP/1.1 401 Unauthorized
Content-Length: 0
Server: Microsoft-IIS/8.0
WWW-Authenticate: Bearer realm="xxxxxxx.azurewebsites.net"
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=697493f3974009aeb448f562d1b389f79aa6008071be1244c77f3f1d3849f5f0;Path=/;Domain=xxxxxxxx.azurewebsites.net
Date: Tue, 24 May 2016 03:59:15 GMT
Hilfe bitte?
Das Hauptproblem, das ich vermute, ist, dass in Azure die AAD-Anwendung keinen Zugriff auf die Web API App hat. Ich habe versucht, es über Benutzer hinzuzufügen, aber wenn ich diese Anwendung als Benutzer suche, kann sie es nicht finden. Die registrierten Anwendungen befinden sich in einem anderen Azure AD Directory.
Haben Sie zufällig die Authentifizierung/Autorisierung im Portal aktiviert? Mehrere Personen sind auf ähnliche Probleme gestoßen, wenn sie sowohl die integrierte Authentifizierungs-/Autorisierungs- als auch die OWIN Azure AD-Authentifizierungs-Middleware in derselben Web-App verwenden. –
Hallo @ Phillippe. Ja, ich habe diese Authentifizierung in der Web App im Portal aktiviert. Ist das nicht notwendig? – Fox
Tut mir leid, @ChrisGillum. Du hast diesen Kommentar gepostet. – Fox