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中的值!

相关问题