我正在研究在基于C#的MVC应用程序中使用Identity Server 4进行身份验证。我想使用Azure AD中存储的帐户作为有效用户的来源,但是文档似乎只涉及Google和OpenID,并且仅在过程中提到了Azure。
是否有人知道如何在使用Identity Server 4时使用Azure AD的任何好文档和/或教程?
我正在研究在基于C#的MVC应用程序中使用Identity Server 4进行身份验证。我想使用Azure AD中存储的帐户作为有效用户的来源,但是文档似乎只涉及Google和OpenID,并且仅在过程中提到了Azure。
是否有人知道如何在使用Identity Server 4时使用Azure AD的任何好文档和/或教程?
您可以像从 JavaScript 或 MVC 应用程序中登录 IdentityServer 一样,使用 IdentityServer 从 Azure AD 中进行登录。
我最近已经做到了这一点,您需要做的就是将 OpenIdConnect 选项注册到 Azure Ad 中,如下所示:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
});
}
然后,在您的登录操作中调用ChallengeAsync方法:
var authenticationProperties = new AuthenticationProperties { RedirectUri = "your redirect uri" };
await HttpContext.Authentication.ChallengeAsync(your policy, authenticationProperties);
然后提供一个回调方法作为GET方法,然后按照IdentityServer示例中提供的外部登录示例进行操作:https://github.com/IdentityServer/IdentityServer4.Samples/blob/dev/Quickstarts/4_ImplicitFlowAuthenticationWithExternal/src/QuickstartIdentityServer/Quickstart/Account/AccountController.cs
有一个在github上使用Azure AD的示例,是从IdentityServer样例中提供的外部登录示例派生而来。
该示例还修复了已知问题"由中间件生成的状态参数对于Azure AD太大#978"
IdentityServer4有一份“使用外部身份提供商登录”的文档。
不幸的是,这份文档并不完整,但我这样做了:
对于.NET 5,请参考Startup.cs
;对于.NET 6,请参考Program.cs
:
services.AddAuthentication()
.AddOpenIdConnect("aad", "Azure AD", options =>
{
options.ClientSecret = "<ClientSecret>";
options.ResponseType = OpenIdConnectResponseType.Code;
options.ClientId ="<ClientId>";
options.Authority = "https://login.microsoftonline.com/<TenantId>/";
options.CallbackPath = "/signin-oidc";
})
.AddIdentityServerJwt();
SignIn.RequireConfirmedAccount = false
来解决这个问题。services.AddDefaultIdentity<ApplicationUser>(options =>
options.SignIn.RequireConfirmedAccount = true)
[dbo].[AspNetUsers]
中将EmailConfirmed
设置为true。Certificates & secrets
下添加客户端密码。