Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

blazor client side .net core how to get username in server api from oidc?

$
0
0

hello i have some blazor projectin client side :

 builder.Services.AddOidcAuthentication(opt => {     opt.ProviderOptions.Authority = "https://x.y.z/oidc";     opt.ProviderOptions.ClientId = "client";     opt.ProviderOptions.DefaultScopes.Add("email");     opt.ProviderOptions.DefaultScopes.Remove("profile");      opt.ProviderOptions.ResponseType = "code";    });  var IHttpServiceClient = builder.Services.AddHttpClient<IBaseHttpService,BaseHttpService>();  builder.Services.AddScoped<CustomAuthorizationMessageHandler>();  IHttpServiceClient.AddHttpMessageHandler<CustomAuthorizationMessageHandler>();and handler is simple:public class CustomAuthorizationMessageHandler:AuthorizationMessageHandler{    public CustomAuthorizationMessageHandler(IAccessTokenProvider provider , NavigationManager nav) :base(provider, nav)    {        ConfigureHandler(authorizedUrls: new[] { nav.BaseUri });    }}

and this way it works authorize as should / redirrect back after login etc , on client side i can

username = (await _authenticationStateProvider.GetAuthenticationStateAsync()).User.Identity!.Name!;`

and i have username / client side is ok.

and now server side / api

 builder.Services .AddAuthentication("Bearer") .AddJwtBearer("Bearer", options => {           options.Authority = `https://x.y.z/oidc`;     options.TokenValidationParameters.ValidateAudience = false;     options.TokenValidationParameters.ValidateIssuer = true; });

and now on some api endpoint i need the same USERNAME from this barer tokenhow to get it?

string username = User.Identity.Name;  // is empty  / screenshot bellow string username = User.Claims.Where(p => p.Type == ClaimTypes.NameIdentifier).First().Value; // is close / please check screenshot bellow

enter image description here why name is null?

and here in claims - NameIdentifier is close to my login but there is some GUID also before login / my login is Emi as on screenshot bellow but i do not want this guid before my nameenter image description here

should i remove this guid and take what is after : ? is there better way?how i should get this login properrly ? In api / from token?

thanks and regards


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>