在 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/