1
我希望我的應用程序在使用Azure網站託管時接受OAuth令牌。我有以下幾點:爲什麼我的Azure網站不接受OAuth令牌?
的web.config的Web應用程序的Web應用程序
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的
<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>
Startup.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());
}
}
任何人都可以提供一些指導?
我假設你可以得到一個正確的標記,是嗎?你從服務中得到什麼錯誤代碼? 一般來說,我看不出這個代碼失敗的原因。你可以訪問你的服務,允許一致訪問任何方法,例如? 順便說一句,您正在使用舊的令牌端點,儘管它不能成爲一個原因(https://login.microsoftonline.com/{0}是新的)。 – Igor
@Igor今天早上纔開始工作......我想我必須讓它坐下來。感謝您對舊端點的關注 – CamronBute