简体   繁体   English

从Azure AD Connect获取身份令牌

[英]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. 我以示例项目为基础,并添加OnTokenValidatedOnTokenResponseReceived进行测试。

(Click on the image to enlarge it) (点击图片放大) 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C# 获取 Azure AD 标识的访问令牌 - C# Get Access Token for Azure AD Identity openid connect owin 如何验证来自 Azure AD 的令牌? - How does the openid connect owin validate the token from Azure AD? Azure AD - 从令牌获取客户端密钥描述 - Azure AD - Get Client Secret Description from Token Azure AD从身份验证结果对象获取访问令牌 - Azure AD Get access token from authentication result object 无法从Azure AD获取承载令牌以与API App一起使用 - Unable to get bearer token from Azure AD to use with API App 如何从 Azure AD 获取 TokenCredentials 的访问令牌? - How to get access token from Azure AD for TokenCredentials? Azure 函数 - 从 DefaultCredentials \ Managed Identity 获取令牌 - Azure Function - Get Token from DefaultCredentials \ Managed Identity 如何使用 Azure AD 令牌身份验证从 WPF 应用程序连接到 Azure SQL Server:避免防火墙 - How to Connect to Azure SQL Server from WPF application with Azure AD token autentication : avoid Firewall 使用OpenId Connect与Azure AD然后模拟现有的身份用户 - Using OpenId Connect with Azure AD then impersonate an existing Identity User 无法在 C# 中获取 Azure AD 令牌 - Not able to get Azure AD token in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM