【问题标题】:How do I secure an ASP.NET MVC Web Api using OWIN/Katana?如何使用 OWIN/Katana 保护 ASP.NET MVC Web Api?
【发布时间】:2014-04-09 18:46:41
【问题描述】:

我正在尝试使用 Windows Azure Active Directory 的 OWIN 提供程序来保护 ASP.NET Web API。我已将 Web api 配置为使用 Windows Azure JWT 不记名令牌,如下所示:

public void Configuration(IAppBuilder app)
{
   app.UseWindowsAzureBearerToken(new WindowsAzureJwtBearerAuthenticationOptions()
     {
       Audience = "http://mywebsitename.azurewebsites.net",
       Tenant = "mydefaultdirectory.onmicrosoft.com"
     });
}

然后,我创建了一个客户端应用程序,该应用程序与 Web api 租户属于同一目录。我指定了授予客户端应用程序访问 Web api 的权限。当我尝试创建客户端应用程序并针对 API 进行身份验证时出现问题。

客户端只是一个简单的windows store应用程序,带有一个按钮点击方法来对windows azure进行身份验证,甚至点击的代码如下:

 private async void myButton_Click(object sender, RoutedEventArgs e)
    {

        AuthenticationContext ac = new AuthenticationContext("https://login.windows.net/nameofazureactivedirectorytenant");
        AuthenticationResult ar =
         await ac.AcquireTokenAsync("api/pathtoresource",
          "xxxxxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxxxx");
        // Call Web API
        string authHeader = ar.CreateAuthorizationHeader();
        HttpClient client = new HttpClient();
        HttpRequestMessage request = new HttpRequestMessage(
          HttpMethod.Get, "http://mywebsitename.azurewebsites.net/api/pathtoresource");
        request.Headers.TryAddWithoutValidation("Authorization", authHeader);
        HttpResponseMessage response = await client.SendAsync(request);
        string responseString = await response.Content.ReadAsStringAsync();
    }

应用程序未能正确创建身份验证上下文,所以我相信我传递了错误的参数。我已经浏览了许多博客文章和 SO 答案,但到目前为止没有任何效果,任何帮助将不胜感激!

【问题讨论】:

    标签: c# asp.net windows azure active-directory


    【解决方案1】:

    查看https://github.com/AzureADSamples 提供的示例。我认为那里有一些应该适用的例子。

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 1970-01-01
      • 2017-05-13
      • 1970-01-01
      • 1970-01-01
      • 2018-12-19
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多