繁体   English   中英

使用OAuth连接到Windows Azure Active Directory

[英]Using OAuth to connect to a Windows Azure Active Directory

在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()行并添加提供程序的密钥和密钥使您能够使用相应的提供程序执行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令牌,如下所示:

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承载身份验证。

快乐的编码!

我不相信你可以这样使用WAAD。 Microsoft Account用于以前的Windows Live ID此处提供更多信息),这与WAAD不同。 WAAD中的OAuth实现尚未完成并在预览中( 此处有更多详细信息 )。 今天使用WAAD的最佳方式是通过WS-Federation / WIF。

VS 2013中的难点在于您无法手动轻松完成,也无法在创建项目后更改所选的身份验证。

获得所需配置的最简单方法是去创建新的Web应用程序,并更改身份验证。 在向导的第一步选择Change Authentication (您可以选择应用程序的类型 - MVC,WebAPI等)。 然后选择Organizational Account 它只有一个选项 - Cloud single organization - 输入您的租户域名(可能是xxxx.onmicrosoft.com )。 并选择了访问级别( Single Sign OnSSO + read directory dataSSO + read + write directory data )。 接下来,系统将要求您使用此Active Directory中的全局管理员帐户登录。 该向导将创建必要的web.confg更改和Identity配置。 在OWIN中仍然不支持WAAD,它将创建一个新的IdentityConfig.cs而不是Startup.Auth.cs文件。 然后,您可以将生成的文件和web.config更改复制到项目中。 您仍然可以将WAAD与其他提供商和OWIN结合使用,但这仍然需要更高级的技能。

应该是它有点复杂。 但事情可能会在未来发生变化。

有一个新的Owin中间件,它使用一些简单的代码行向您的站点添加Ws Federation身份验证,就像新的MVC项目模板中的个人帐户示例一样。 它目前处于alpha版本,但这里是一篇文章的链接 ,该文章解释了如何在Windows Azure Active Directory中创建应用程序并配置OWIN中间件。

但是,这使用cookie身份验证而不是OAuth令牌,但这对于纯ASP MVC站点来说应该足够了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM