针对 Azure AD 的 WebForms 身份验证
WebForms authentication against Azure AD
我有一个 WebForms 站点 运行 在内部服务器上并根据我们的内部 Active Directory 对用户进行身份验证。由于我们正在实施的一些新功能,此站点需要移动到外部服务器,然后更改身份验证,以便它根据我们的 Office 365 帐户对用户进行身份验证。为此我有:
- 创建了一个新的 WebForms 站点(未使用 MVC)
- 在 Azure 中设置一个新的应用程序。
修改Startup.Auth.cs如下:
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions { ClientId = "MyApplicationGUID", Authority = "https://login.windows.net/MyDomain.com" });
当我转到默认页面并单击“登录”时,它会将我带到正确的登录页面并显示 OpenID 按钮。如果我单击该按钮,我将转到 Microsoft 登录页面,我可以在其中输入我的凭据。但是,此时,我被重定向回我网站的登录页面,它仍然要求 username/password.
我想做的是设置站点,以便如果用户未通过身份验证,他们将直接重定向到 Microsoft 登录页面,并在成功登录后重定向回他们最初请求的页面。如果做不到这一点,我会对让默认登录页面正常工作感到满意,这样当我单击 OpenID 时,我就不会被重定向回登录页面。
我现在没有时间学习 MVC 并移植整个东西,所以现在走这条路不是一个选择。
我对这个过程了解不够,所以如果我的问题没有意义或者您需要更多信息,请告诉我,我很乐意尝试找到您需要的帮助我在里面。
也许我遗漏了什么,但我不明白您为什么需要自定义登录页面或外部登录 cookie。 OIDC/AAD 的典型 Startup.Auth 看起来像这样:
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = "AppGUID",
Authority = "https://login.windows.net/MyDomain.com",
// After authentication return user to the page they were trying
// to access before being redirected to the Azure AD signin page.
Notifications = new OpenIdConnectAuthenticationNotifications()
{
RedirectToIdentityProvider = (context) =>
{
string currentUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.Path;
context.ProtocolMessage.RedirectUri = currentUrl;
return Task.FromResult(0);
}
}
});
cookie 身份验证只是为了防止每个请求都转到 AAD。所有真正的工作都发生在 OpenIdConnectAuthentication 中。
以下是 WebForms、Azure AD 和 OpenID Connect 的示例:
我有一个 WebForms 站点 运行 在内部服务器上并根据我们的内部 Active Directory 对用户进行身份验证。由于我们正在实施的一些新功能,此站点需要移动到外部服务器,然后更改身份验证,以便它根据我们的 Office 365 帐户对用户进行身份验证。为此我有:
- 创建了一个新的 WebForms 站点(未使用 MVC)
- 在 Azure 中设置一个新的应用程序。
修改Startup.Auth.cs如下:
public void ConfigureAuth(IAppBuilder app) { app.CreatePerOwinContext(ApplicationDbContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create); app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login"), Provider = new CookieAuthenticationProvider { OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>( validateInterval: TimeSpan.FromMinutes(30), regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)) } }); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions { ClientId = "MyApplicationGUID", Authority = "https://login.windows.net/MyDomain.com" });
当我转到默认页面并单击“登录”时,它会将我带到正确的登录页面并显示 OpenID 按钮。如果我单击该按钮,我将转到 Microsoft 登录页面,我可以在其中输入我的凭据。但是,此时,我被重定向回我网站的登录页面,它仍然要求 username/password.
我想做的是设置站点,以便如果用户未通过身份验证,他们将直接重定向到 Microsoft 登录页面,并在成功登录后重定向回他们最初请求的页面。如果做不到这一点,我会对让默认登录页面正常工作感到满意,这样当我单击 OpenID 时,我就不会被重定向回登录页面。
我现在没有时间学习 MVC 并移植整个东西,所以现在走这条路不是一个选择。
我对这个过程了解不够,所以如果我的问题没有意义或者您需要更多信息,请告诉我,我很乐意尝试找到您需要的帮助我在里面。
也许我遗漏了什么,但我不明白您为什么需要自定义登录页面或外部登录 cookie。 OIDC/AAD 的典型 Startup.Auth 看起来像这样:
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = "AppGUID",
Authority = "https://login.windows.net/MyDomain.com",
// After authentication return user to the page they were trying
// to access before being redirected to the Azure AD signin page.
Notifications = new OpenIdConnectAuthenticationNotifications()
{
RedirectToIdentityProvider = (context) =>
{
string currentUrl = context.Request.Scheme + "://" + context.Request.Host + context.Request.Path;
context.ProtocolMessage.RedirectUri = currentUrl;
return Task.FromResult(0);
}
}
});
cookie 身份验证只是为了防止每个请求都转到 AAD。所有真正的工作都发生在 OpenIdConnectAuthentication 中。
以下是 WebForms、Azure AD 和 OpenID Connect 的示例: