на стороне клиента:
Код: Выделить всё
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();
builder.Services.AddScoped();
IHttpServiceClient.AddHttpMessageHandler();
and handler is simple:
public class CustomAuthorizationMessageHandler:AuthorizationMessageHandler
{
public CustomAuthorizationMessageHandler(IAccessTokenProvider provider , NavigationManager nav) :base(provider, nav)
{
ConfigureHandler(authorizedUrls: new[] { nav.BaseUri });
}
}
Код: Выделить всё
username = (await _authenticationStateProvider.GetAuthenticationStateAsync()).User.Identity!.Name!;`
а теперь серверная часть/api
Код: Выделить всё
builder.Services
.AddAuthentication("Bearer")
.AddJwtBearer("Bearer", options =>
{
options.Authority = `https://x.y.z/oidc`;
options.TokenValidationParameters.ValidateAudience = false;
options.TokenValidationParameters.ValidateIssuer = true;
});
как его получить?< /p>
Код: Выделить всё
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

почему имя имеет значение null?
и здесь в претензиях — NameIdentifier близок к моему логину, но перед входом в систему также есть некоторый GUID / мой логин — Emi, как на скриншоте ниже, но я не хочу, чтобы этот GUID стоял перед моим именем

должен ли я удалить это руководство и взять то, что это после : ? есть ли лучший способ?
как мне правильно получить этот логин? В API/из токена?
Спасибо и с уважением
--------------редактирую
в интерфейсе, когда я это делаю
Код: Выделить всё
@foreach (var claim in @context.User.Claims)
{
@claim.Type : @claim.Value
}
Код: Выделить всё
auth_time : 171xxx7995
jti : c9e59bxxxxxxxx77a38e4a
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier : f:2819xxxxxxxxx2583c6c9df:Emi
typ : ID
session_state : 331984xxxxxxxx2918950b
sid : 331984dxxxxxxxxxxx8950b
email_verified : false
http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress : xxx.emixx@xxxxe.com
name : emi
Подробнее здесь: https://stackoverflow.com/questions/784 ... -from-oidc