c# - 使用 OAuth 连接到 Windows Azure Active Directory

标签 c# asp.net azure oauth

在 ASP.NET MVC 项目的脚手架中,StartUp.Auth.cs 文件当前包含以下代码:

public partial class Startup
{
    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
    public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
        // Use a cookie to temporarily store information about a user logging in with a third party login provider
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

        // Uncomment the following lines to enable logging in with third party login providers
        app.UseMicrosoftAccountAuthentication(
            clientId: "0000000000000000",
            clientSecret: "xxxx-xxxxxxxxxxxxxxxxxxx-xxxxxxx");

        //app.UseTwitterAuthentication(
        //   consumerKey: "",
        //   consumerSecret: "");

        //app.UseFacebookAuthentication(
        //   appId: "",
        //   appSecret: "");

        //app.UseGoogleAuthentication();
    }
}

取消注释 app.UseXxxAuthentication() 行并添加提供商的 key 和 secret ,使您能够使用相应的提供商执行 OAuth 登录。在幕后,这些方法使用从 Owin 类 AuthenticationMiddleware 派生的类。

我在网上查看过,但找不到直接链接到 Windows Azure Active Directory 实例的 AuthenticationMiddleware 的自定义实现。有这样的实现吗?

这是使用 OAuth 连接到我的 Windows Azure Active Directory 实例的正确方法吗?

最佳答案

您应该能够转到程序包管理器,然后 NuGet 导入 Windows Azure AD 的 Katana Owin 实现,该实现将列为 Microsoft.Owin.Security.ActiveDirectory 这是使应用程序能够使用 Microsoft 技术的中间件用于身份验证。截至本文的当前版本是 2.0.2

一旦有了这个,您应该能够利用 AD 和 ADFS 2.1 oAuth token 的中间件,如下所示:

WindowsAzureActiveDirectoryBearerAuthenticationOptions myoptions = new WindowsAzureActiveDirectoryBearerAuthenticationOptions();
        myoptions.Audience = "https://login.windows.net/myendpoint";
        myoptions.Tenant = "mydirectory.onmicrosoft.com";
        myoptions.AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Passive;
        app.UseWindowsAzureActiveDirectoryBearerAuthentication(myoptions);

这应该使您能够让 Owin 中间件在这种情况下使用 Windows Azure AD 承载身份验证。

祝你编码愉快!

关于c# - 使用 OAuth 连接到 Windows Azure Active Directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20660356/

相关文章:

asp.net - 针对整个框架的项目的asp.net core 2.0 docker镜像

c# - linq to sql中如何判断是否使用join?

c# - 使用 Azure 存储模拟器测试开发

azure - 简单选择计数(id) 使用 100% 的 Azure SQL DTU

c# - 套接字编程 - 连接尝试失败异常

c# - 通过鼠标拖动或使用鼠标滚轮水平滚动的面板

asp.net - 使用 ODBC 3.51 将 ASP Visual Web Developer 2008 连接到 MySQL

c# - Entity Framework - 如何不将继承的类添加到数据库

c# - 代码优先迁移中的固定长度字符串列

azure - 在 azure 管道中将文件从一个作业模板移动或复制到另一个作业模板