azure - 为什么我的 Azure 网站不接受 OAuth token ?

标签 azure oauth claims-based-identity azure-active-directory

我希望我的应用程序在使用 Azure 网站托管时接受 OAuth token 。我有以下内容:

网络应用程序的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 中看到的值完全相同!

关于azure - 为什么我的 Azure 网站不接受 OAuth token ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30406397/

相关文章:

python - 如何从 Django 发推文?

asp.net-mvc - 如何从 Azure ACS 获取电子邮件和姓名?

azure - 如何更新 Azure Blob 存储中的文件上次修改日期

azure - 使用 Powershell,如何向 azure api 管理 API 添加版本?

azure - 如何使用 Azure PowerShell SDK 将所需的权限添加到 Azure Active Directory (AAD) 应用程序?

c# - OWIN ASP.NET - 如果访问 token 已过期,则无法使用刷新 token 生成访问 token

laravel - 使用 OAuth 时在移动设备上存储 'client_secret' 是否安全?

Asp.Net Identity - 在运行时设置 CookieDomain

c# - ASP.NET Identity 不会根据同一请求更新身份信息

css - 如何更改机器人背景颜色