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()); 
    } 
} 

任何人都可以提供一些指導?

+0

我假設你可以得到一個正確的標記,是嗎?你從服務中得到什麼錯誤代碼? 一般來說,我看不出這個代碼失敗的原因。你可以訪問你的服務,允許一致訪問任何方法,例如? 順便說一句,您正在使用舊的令牌端點,儘管它不能成爲一個原因(https://login.microsoftonline.com/{0}是新的)。 – Igor

+0

@Igor今天早上纔開始工作......我想我必須讓它坐下來。感謝您對舊端點的關注 – CamronBute

回答

0

所以事實證明,我使用Uri類來驗證App ID的URI。問題是,它在尾部添加了尾部斜線,這會導致問題。只要我開始使用string類來存儲App ID URI,就沒有問題。

因此,請確保您使用的是Azure AD中的值!

相關問題