首页 > 解决方案 > 从 Blazor 客户端访问 IdentityUser

问题描述

我目前正在使用 Blazor WebAssembly、ASP.NET Core 托管、EF/Identity Core。

我想在我的客户端 Blazor 页面中填写国家和另一位用户的下拉列表。

Country很简单,因为它的模型在\Shared\Models\Country.cs.

我很难携带UserIdentity,因为它是 Microsoft Identity 的一部分,我无法放入我的\Shared\Models. 所以,编译器不能识别实体,我不能带它的命名空间。

关于如何填充用户下拉列表的任何想法?也许创建一个名为\Shared\Models\AppUser.cs继承自的虚拟类UserIdentity就足够了?

或者也许添加对服务器项目的引用?

这是示例代码:

@page "/"
@using BlazorApp.Shared.Models
@inject HttpClient Http

<h1>Dropdown Example</h1>

@if (countryList == null || userList == null )
{
    <p><em>Loading...</em></p>
}
else
{
<select class="form-control">
    <option value="">-- Select Country --</option>
    @foreach (var country in countryList)
    {
        <option value="@country.Id">@country.CountryName</option>
    }
</select>

<select class="form-control">
    <option value="">-- Select User --</option>
    @if (userList != null)
    {
        @foreach (var user in userList)
        {
            <option value="@user.Id">@user.UserName</option>
        }
    }
</select>
}

@functions {
    List<Country> countryList = new List<Country>();
    List<IdentityUser> userList = new List<IdentityUser>();

    protected override async Task OnInitAsync() {
        countryList = await Http.GetJsonAsync<List<Country>>("api/country/GetCountryList");
        userList = await Http.GetJsonAsync<List<IdentityUser>>("api/user/GetUserList");
    }
}

标签: blazorasp.net-core-identity

解决方案


推荐阅读