【发布时间】:2021-03-31 11:47:25
【问题描述】:
你好社区我有一个问题,我如何在 blazor 页面 webassembly 中使用 usermanager ?通过注入:
@inject UserManager<ApplicationUser> UserManager;
我得到指示缺少使用指令,因为 ApplicationUser 类在服务器上,并且客户端无权访问服务器。
这是我在 blazor 页面中的代码:
@page "/index"
@inject AuthenticationStateProvider AuthenticationStateProvider
@using Microsoft.AspNetCore.Identity;
@inject UserManager<ApplicationUser> UserManager;
<button @onclick="@LogUsername">Write user info to console</button>
<br />
<br />
@Message
@code {
string Message = "";
private async Task LogUsername()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
var currentUser = await UserManager.GetUserAsync(user);
Message = ($"{user.Identity.Name} is authenticated.{ currentUser.Nombre }");
}
else
{
Message = ("The user is NOT authenticated.");
}
}
}
这是我的班级 ApplicationUser:
public class ApplicationUser: IdentityUser
{
public string Nombre { get; set; }
public string ApellidoPaterno { get; set; }
public string ApellidoMaterno { get; set; }
public virtual Cliente InquilinoActual { get; set; }
}
【问题讨论】:
-
github.com/BrianLParker/AuthApp 自定义应用程序用户并将其作为声明传递....
标签: c# asp.net-web-api blazor blazor-webassembly usermanager