【发布时间】:2017-03-02 20:39:00
【问题描述】:
我很难理解 IdentityServer3、AzureAD 和私有数据库如何协同工作。最大的问题是如何处理重定向 URI。
我的情况是我有一个独立的 IdentityServer3。它的工作是针对 AzureAD 或私有 DB 对用户进行身份验证。在 ID3 服务器上的 Startup.cs 文件中,我有以下 OpenID Connect 代码:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.Map("/identity", s3App =>
{
s3App.UseIdentityServer(new IdentityServerOptions
{
SiteName = "3S",
SigningCertificate = Certificate.Load(),
Factory = new IdentityServerServiceFactory()
.UseInMemoryUsers(InMemoryUsers.Get())
.UseInMemoryClients(InMemoryClients.Get())
.UseInMemoryScopes(InMemoryScopes.Get()),
AuthenticationOptions = new AuthenticationOptions
{
EnablePostSignOutAutoRedirect = true,
EnableSignOutPrompt = false,
IdentityProviders = ConfigureAdditionalIdentityProviders
}
});
});
}
public static void ConfigureAdditionalIdentityProviders(IAppBuilder app, string signInAsType)
{
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
AuthenticationType = "AzureAd",
Caption = "Login",
ClientId = "4613ed32-xxxx-xxxx-xxxx-xxxxxxxxxxxx", // GUID of registered application on Azure
Authority = "https://login.microsoftonline.com/our-tenant-id/",
PostLogoutRedirectUri = "https://localhost:44348/identity",
RedirectUri = "https://localhost:44348/identity",
Scope = "openid email profile",
ResponseType = "id_token",
AuthenticationMode = AuthenticationMode.Passive,
SignInAsAuthenticationType = signInAsType,
TokenValidationParameters = new TokenValidationParameters
{
AuthenticationType = Constants.ExternalAuthenticationType,
ValidateIssuer = false
}
});
}
我不明白为什么 ID3 服务器需要有 RedirectUri 或 PostLogoutRedirectUri...不应该从请求身份验证的应用程序“通过”吗?毕竟,我们想要回到应用程序,而不是 ID3 服务器。诚然,我不认为这是导致我的问题的原因,但很高兴能理解为什么会出现这些问题。
我会说,我已经“接近”了这项工作。
当我需要身份验证的应用程序请求对 AzureAD 进行身份验证时,我将被重定向到 Microsoft 帐户登录屏幕,以输入我的工作帐户的用户名/密码。我提交凭据,然后重定向回 ID3 服务器或我的应用程序,具体取决于上述代码中使用的 RedirectUri。
为了论证,假设我将我的应用程序用于 RedirectUri。我将被发送回应用程序,而不是最初提示身份验证质询的页面,如果我单击需要身份验证的页面,我将被发送回 AzureAD 服务器再次登录,只是这次 AzureAD认出我已经登录了。
不幸的是,在从 AzureAD 重定向后,SecurityTokenValidated 通知似乎没有被确认/设置。
这是在应用程序 Startup.cs 中找到的代码:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies"
});
app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions
{
Authority = "https://localhost:44348/identity",
ClientId = "3af8e3ba-5a04-4acc-8c51-1d30f8587ced", // Local ClientID registered as part of the IdentityServer3 InMemoryClients
Scope = "openid profile roles",
RedirectUri = "http://localhost:52702/",
PostLogoutRedirectUri = "http://localhost:52702/",
ResponseType = "id_token",
SignInAsAuthenticationType = "Cookies",
UseTokenLifetime = false,
Notifications = new OpenIdConnectAuthenticationNotifications
{
SecurityTokenValidated = n =>
{
var id = n.AuthenticationTicket.Identity;
var givenName = id.FindFirst(Constants.ClaimTypes.GivenName);
var familyName = id.FindFirst(Constants.ClaimTypes.FamilyName);
var sub = id.FindFirst(Constants.ClaimTypes.Subject);
var roles = id.FindAll(Constants.ClaimTypes.Role);
var nid = new ClaimsIdentity(
id.AuthenticationType,
Constants.ClaimTypes.GivenName,
Constants.ClaimTypes.Role
);
nid.AddClaim(givenName);
nid.AddClaim(familyName);
nid.AddClaim(sub);
nid.AddClaims(roles);
nid.AddClaim(new Claim("application_specific", "Some data goes here. Not sure what, though."));
nid.AddClaim(new Claim("id_token", n.ProtocolMessage.IdToken));
n.AuthenticationTicket = new AuthenticationTicket(nid, n.AuthenticationTicket.Properties);
return Task.FromResult(0);
},
RedirectToIdentityProvider = n =>
{
if (n.ProtocolMessage.RequestType != OpenIdConnectRequestType.LogoutRequest)
return Task.FromResult(0);
var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token");
if (idTokenHint != null)
{
n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
}
return Task.FromResult(0);
},
AuthenticationFailed = (context) =>
{
context.HandleResponse();
context.Response.Redirect("/Error/message=" + context.Exception.Message);
//Debug.WriteLine("*** AuthenticationFailed");
return Task.FromResult(0);
},
}
});
AntiForgeryConfig.UniqueClaimTypeIdentifier = Constants.ClaimTypes.Subject;
JwtSecurityTokenHandler.InboundClaimTypeMap = new Dictionary<string, string>();
}
}
您会注意到 OpenIdConnectAuthenticationOptions 还包含指向应用程序的 RedirectUri 和 PostLogoutRedirectUri,但这些似乎无关紧要。
当然,当我使用事物的“cookies”端登录时,一切正常 - 我看到了我对用户的所有声明。而且,花了一些时间与微软通电话,他们提出了一个 ID3 之外的解决方案,它有效,但不是我们需要走的路。我们将有多个应用程序根据我们的 ID3 进行身份验证,因此我们需要在内部包含和控制流程。
我真的需要一些帮助来解决这个最后一英里的问题。我知道我已经接近了,我一直盯着这个太久了,以至于我可能正盯着我的错误而没有看到它。
2016 年 10 月 22 日编辑
进一步的测试和启用 Serilog 揭示了 RedirectUri 和 PostLogoutRedirectUri 的问题,导致我将 /identity 添加到 URI 的末尾,这与 app.Map 中设置的值相对应。这解决了我返回到 IdentityServer3 的“空白”页面的问题,我现在返回到 IdentityServer3 登录屏幕。 Azure AD 仍然认为我已登录,我只是没有在我的应用程序中正确设置令牌。
【问题讨论】:
标签: c# asp.net authentication azure-active-directory identityserver3