[英]Get the identity token from Azure AD Connect
I am trying to find out how can I get the Identity Token from the azure ad connect. 我试图找出如何从azure广告连接获取身份令牌。 I am integrating it with Identity Server 4 (dotnet core).
我正在将其与Identity Server 4(dotnet核心)集成。 Their sample shows how to connect AD with Identity Server but I can't find how actually get the Id Token.
他们的示例显示了如何将AD与Identity Server连接,但是我找不到实际如何获得Id令牌的方法。 I've also tried accessing it using the events but had no success.
我也尝试使用事件访问它,但没有成功。 This is my configuration on Startup.cs on the identity server project.
这是我在身份服务器项目上的Startup.cs上的配置。
public void Configure(IApplicationBuilder app, IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(LogLevel.Debug);
app.UseDeveloperExceptionPage();
app.UseIdentityServer();
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
AutomaticAuthenticate = false,
AutomaticChallenge = false
});
///
/// Setup Custom Data Format
///
var schemeName = "oidc";
var dataProtectionProvider = app.ApplicationServices.GetRequiredService<IDataProtectionProvider>();
var distributedCache = app.ApplicationServices.GetRequiredService<IDistributedCache>();
var dataProtector = dataProtectionProvider.CreateProtector(
typeof(OpenIdConnectMiddleware).FullName,
typeof(string).FullName, schemeName,
"v1");
var dataFormat = new CachedPropertiesDataFormat(distributedCache, dataProtector);
///
/// Azure AD Configuration
///
var clientId = "XXXX";
var tenantId = "XXXXX";
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = schemeName,
DisplayName = "AzureAD",
SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
ClientId = clientId,
Authority = $"https://login.microsoftonline.com/{tenantId}",
ResponseType = OpenIdConnectResponseType.IdToken,
StateDataFormat = dataFormat,
Events = new OpenIdConnectEvents
{
OnRemoteFailure = OnAuthenticationFailed,
OnTokenValidated = OnTokenValidated,
OnTokenResponseReceived = TokenResponseReceived
},
TokenValidationParameters = new TokenValidationParameters
{
SaveSigninToken = true
}
});
app.UseStaticFiles();
app.UseMvcWithDefaultRoute();
}
These are my event handlers from which I hoped to get the id token. 这些是我希望从中获取ID令牌的事件处理程序。
private Task OnTokenValidated(TokenValidatedContext context)
{
var type = context.Properties.GetType();
var tokens = context.Properties.GetTokens();
var ci = (System.Security.Claims.ClaimsIdentity)
ClaimsPrincipal.Current.Identity;
return Task.FromResult(0);
}
private Task OnAuthenticationFailed(FailureContext context)
{
var failure = context.Failure;
return Task.FromResult(0);
}
public Task TokenResponseReceived(TokenResponseReceivedContext context)
{
var variable = context.TokenEndpointResponse.IdToken;
return Task.FromResult(0);
}
You can read Token information under context.SecurityToken
. 您可以在
context.SecurityToken
下读取令牌信息。
I use my sample project as base, and add OnTokenValidated
and OnTokenResponseReceived
to test it. 我以示例项目为基础,并添加
OnTokenValidated
和OnTokenResponseReceived
进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.