我希望我的应用程序在使用Azure网站托管时接受OAuth令牌。我有以下内容:
Web应用程序的web.config
<appSettings>
<add key="ida:Realm" value="https://example.com/development" />
<add key="ida:AudienceUri" value="https://example.com/development" />
<add key="ida:Tenant" value="example.com" />
</appSettings>
Web应用程序的Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Security.ActiveDirectory;
using System.Configuration;
[assembly: OwinStartup(typeof(MyApplication.Web.Startup))]
namespace MyApplication.Web
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters()
{
ValidAudience = ConfigurationManager.AppSettings["ida:AudienceUri"]
},
Tenant = ConfigurationManager.AppSettings["ida:Tenant"]
});
}
}
}
Main.cs
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Globalization;
using System.Net.Http;
using System.Net.Http.Headers;
void Main()
{
var clientId = @"GUIDGUIDGUID";
var key = @"KEYKEYKEYKEYKEY";
var aadInstance = "https://login.windows.net/{0}";
var tenant = "example.com";
var authContext = new AuthenticationContext(String.Format(CultureInfo.InvariantCulture, aadInstance, tenant), true);
var credential = new ClientCredential(clientId, key);
authContext.TokenCache.Clear();
var token = authContext.AcquireToken(@"https://example.com/development", credential);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.AccessToken);
var response = client.GetAsync(@"https://app.example.com/").Result;
var responseText = response.Content.ReadAsStreamAsync().Result;
Console.Write(new StreamReader(responseText).ReadToEnd());
}
}
谁能提供一些指导?
因此,事实证明我正在使用Uri
该类来验证App ID URI。问题是,它在末尾添加了斜杠,从而导致了问题。一旦我开始使用string
该类来存储App ID URI,就很好了。
因此,请确保您完全使用Azure AD中显示的值!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句