@page "/userprofile"
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject IJSRuntime JSRuntime
@code {
protected override async Task OnInitializedAsync()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var userId = authState.User.FindFirstValue(ClaimTypes.NameIdentifier);
await JSRuntime.InvokeVoidAsync("setProperty", "userId", userId);
}
}
在上面的代码示例中,通过调用 IJSRuntime 中的 InvokeVoidAsync() 方法,使用 JavaScript 中的 setProperty() 函数将当前用户的 ID 值设置为隐藏输入元素的值。