2017-02-01 39 views
9

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

有没有人知道有关如何在使用Identity Server 4的环境中使用Azure AD的良好文档和/或教程?

回答

5

您可以使用从IdentityServer登录到Azure AD,就像您使用登录身份验证服务的身份验证服务一样。一个Javascript或MVC应用程序。

我最近做了这一点,所有你需要做的是注册OpenIdConnect选项Azure的广告是这样的:这个

public void ConfigureAuth(IAppBuilder app) 
{ 
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); 

    app.UseCookieAuthentication(new CookieAuthenticationOptions()); 

    app.UseOpenIdConnectAuthentication(
     new OpenIdConnectAuthenticationOptions 
     { 
      ClientId = clientId, 
      Authority = authority, 
      PostLogoutRedirectUri = postLogoutRedirectUri, 
     }); 
} 

此处了解详情:https://docs.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方法,然后按照Id中提供的外部登录样本entityServer样本:https://github.com/IdentityServer/IdentityServer4.Samples/blob/dev/Quickstarts/4_ImplicitFlowAuthenticationWithExternal/src/QuickstartIdentityServer/Quickstart/Account/AccountController.cs

+0

谢谢您的信息。我只是从IS4开始,所以我需要问你一些事情:我阅读关于外部登录的文档,并且示例向您显示了一个按钮,我想它会将您重定向到Google身份验证页面,因此我还假设您的示例重定向到微软登录页面,还是我错了?如果这是真的,是否可以自动验证用户知道他的凭据?我的意思是,有人向我的JS应用程序发送用户的凭据(微软),然后我将它们发送给IS4,它试图获得令牌 – FacundoGFlores

+2

这确实是OpenID Connect的要点;您将处理密码的麻烦委派给另一个系统,在这种情况下是Microsoft。您将不得不添加像login.microsoft.com或类似的权限,然后这将在重定向时使用。 OpenID Connect不是Microsoft特定的;这仅仅是一个规范,如何与第三方提供商登录应该发生。 –

6

有一个sample with Azure AD on github,从IdentityServer samples提供的外部登录样本中分出。

该示例还修复了一个已知问题"State parameter generated by middleware is too large for Azure AD #978"

+1

https://github.com/aspnet/Security/issues/1310 上面给出的代码示例现在已经过时了,来自Haok的评论“旧1.0认证栈不再适用,并且在2.0中已经过时”。这特别涉及上面的回购集IdentityServer startup.cs的配置/ cookie身份验证的方式。这个迁移是在6月17日左右完成的,我可以告诉你。 – JakeJ

+0

如果你放在app.UseCookieAuthentication中,然后看构造函数的注释,它会带你到上面的链接。我仍然赞成你的帮助,如果我能够从单独的回购中插入我需要的代码,那将是非常棒的。 – JakeJ

+0

@JakeJ,你可以在GitHub存储库上创建一个问题,要求升级到Core 2.0,甚至提交一个Pull Request。 –