Identityserver 4和Azure AD

35

我正在研究在基于C#的MVC应用程序中使用Identity Server 4进行身份验证。我想使用Azure AD中存储的帐户作为有效用户的来源,但是文档似乎只涉及Google和OpenID,并且仅在过程中提到了Azure。

是否有人知道如何在使用Identity Server 4时使用Azure AD的任何好文档和/或教程?

3个回答

20

您可以像从 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,
        });
}

更多关于此的信息请参见:https://learn.microsoft.com/en-us/azure/active-directory/develop/active-directory-devquickstarts-webapp-dotnet

然后,在您的登录操作中调用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


谢谢您提供的信息。我刚开始使用IS4,所以需要问您一些问题:我阅读了有关外部登录的文档,示例显示了一个按钮,我想它会将您重定向到Google身份验证页面,因此我认为您的示例会将您重定向到Microsoft登录页面,或者我错了吗?如果是真的,是否可以自动验证用户并知道他的凭据?我的意思是,有人向我的JS应用程序发送用户的凭据(Microsoft),然后我将它们发送到IS4,并尝试获取令牌。 - FacundoGFlores
2
这就是OpenID Connect的关键所在;您将密码处理的烦恼委托给另一个系统,例如Microsoft。您需要添加类似于login.microsoft.com的授权,然后在重定向时使用它。OpenID Connect并不是Microsoft特有的;它只是规定了如何与第三方提供商进行登录的规范。 - Espen Medbø

10

4
上面给出的代码示例已经过时,根据Haok的评论:“旧的1.0身份验证堆栈将不再工作,并且在2.0中已经过时”。这特别涉及到上述仓库设置IdentityServer startup.cs的Config / cookie身份验证的方式。迁移大约是在6月17日完成的,据我所知。 - JakeJ
1
如果您在app.UseCookieAuthentication中输入代码,则可以查看构造函数上的注释,它将带您转到上面的链接。我仍然为您的帮助点赞,如果我能够仅从该存储库中插入所需的代码,那将是很棒的事情。 - JakeJ
@JakeJ,你可以在GitHub存储库上创建一个问题,请求升级到Core 2.0甚至提交一个Pull Request。 - Michael Freidgeim

2

IdentityServer4有一份“使用外部身份提供商登录”的文档。

http://docs.identityserver.io/en/latest/topics/signin_external_providers.html#state-url-length-and-isecuredataformat

不幸的是,这份文档并不完整,但我这样做了:

对于.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();

您将在“使用其他服务登录”下看到一个外部登录链接: enter image description here 完成登录后,您应该会看到这个消息。 enter image description here 点击“注册”后,默认设置被卡住了。这是因为新电子邮件没有得到确认。可以通过设置SignIn.RequireConfirmedAccount = false来解决这个问题。
services.AddDefaultIdentity<ApplicationUser>(options => 
    options.SignIn.RequireConfirmedAccount = true)

您也可以使用“重新发送电子邮件确认”或在新用户的[dbo].[AspNetUsers]中将EmailConfirmed设置为true。
Azure AD设置。您还需要在Certificates & secrets下添加客户端密码。 enter image description here enter image description here

哪个示例/版本提供了您上面发布的模板?我的意思是带有“关联您的AZURE AD帐户”消息的那个 - 是您创建的吗?我之所以问是因为我认为我从未在任何官方示例中看到过它。发布源代码将会很有帮助。谢谢。 - rosko
1
@rosko 创建一个新的 Blazor WebAssembly 应用程序,包含个人用户账户,并且您将收到模板代码。 - Ogglas

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接