Search code examples
c#asp.netasp.net-coreasp.net-identityidentityserver4

How to find Client id in login flow


I have an Identity server 4 application using .net core. Everything is working fine. What i would like to do is Log the client that the user is logging in with as we have a number of third party applications and would like to track user login based upon application.

When a user hits the login form they get the following

http://localhost:5000/account/login?returnUrl=%2Fconnect%2Fauthorize%2Fcallback%3Fclient_id%3DClient%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%253A49000%252Fsignin-oidc%26response_mode%3Dform_post%26response_type%3Did_token%2520token%2520code%26scope%3Dopenid%2520profile%2520testapi%26state%3DOpenIdConnect.AuthenticationProperties%253DEf4ItTF_eWXPU2OTCYP3CqKsds3ywXrsYSfwxnFQCa-p9LYjfJYPXl6OIJWlKVKAyyN1o_5zeox2Wff5SlXEasQ8r44igT72kaDTzUevTOFwh1pkQyDe9Cwxes3pmFNJJOtet2WRON9XnGkabWtuDYnTbumSqyI4pG_zgb6SsY9A6Fnd-rAPSWFPhsLNVJUXY9PRiw%26nonce%3D636740608058134855.YzdiZmQ2MDYtZmY0Zi00MWZjLTg2NmMtMTIxOGMxMDBlODgxNDZhY2Y1ODQtODNhYi00Yzc4LWIyMDQtYTE2MzhkZWMwYmIy

The client id is in the url and I can see it coming though some of the Identity server 4 middle were logging. What i cant figure out is how to grab it out of HttpContext or some other variable that i have not been able to find. I have checked signinmanager as well and cant seem to find it there either.

Does anyone know where i can find the client id that of the application that the user is logging in with?


Solution

  • You can use the IIdentityServerInteractionService to get the AuthorizationRequest this object contains all the data about the current authorization request, including the ClientId property.

    var context = await _interaction.GetAuthorizationContextAsync(returnUrl);
    var clientId = context.ClientId;