无法在 AspNetCore 3.1 的视图中注入扩展的 IdentityUser
Failed to Inject extended IdentityUser in view in AspNetCore 3.1
我创建了一个扩展 IdentityUser
的 ApplicationUser
。现在我想 show/hide 登录和注销视图。因此,我尝试在 _signinPartial.cshtml
视图中注入 ApplicationUser
,但我收到 ApplicationUser 错误。
它说 the type of namespace ApplicationUser can not be found(are you missing a using directive or an assembly reference)
。
我在互联网上找到了多个博客,他们做的和我做的完全一样。
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
}
我在 Startup.cs
中这样配置 ApplicationUser
:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>();
我的 _LoginPartial.cshtml 看起来像:
@inject SignInManager<ApplicationUser> signInManager
@if (signInManager.IsSignedIn(User))
{
<li class="nav-item">
<form method="post" asp-controller="account" asp-action="logout">
<button type="submit" style="width:auto"
class="nav-link btn btn-link py-0">
Logout @User.Identity.Name
</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link" asp-controller="account" asp-action="index">
Login
</a>
</li>
}
我在 _ViewImports.cshtml
视图中导入了 Microsoft.AspNetCore.Identity
和 OAuth2Demo.Models
。
当我注入 IdentityUser
而不是 ApplicationUser
时,我可以完美地工作。我可以使用 ApplicationUser 执行其他操作,例如将用户存储在数据库中,为其他目的获取声明。
the type of namespace ApplicationUser can not be found(are you missing a using directive or an assembly reference).
I've imported Microsoft.AspNetCore.Identity and OAuth2Demo.Models in _ViewImports.cshtml view
要解决此问题,请检查您的 ApplicationUser
class 是否定义如下。
namespace OAuth2Demo.Models
{
public class ApplicationUser: IdentityUser
{
//...
//properties here
//...
请注意,_ViewImports.cshtml
文件可以放在任何文件夹中,在这种情况下,它只会应用于该文件夹中的页面或视图及其子文件夹。因此,请确保您在正确的 _ViewImports.cshtml
文件中添加 @using OAuth2Demo.Models
。
此外,您可以尝试在_signinPartial.cshtml
中指定一个完全限定的名称,然后检查它是否可以正常工作。
@inject SignInManager<OAuth2Demo.Models.ApplicationUser> signInManager
我创建了一个扩展 IdentityUser
的 ApplicationUser
。现在我想 show/hide 登录和注销视图。因此,我尝试在 _signinPartial.cshtml
视图中注入 ApplicationUser
,但我收到 ApplicationUser 错误。
它说 the type of namespace ApplicationUser can not be found(are you missing a using directive or an assembly reference)
。
我在互联网上找到了多个博客,他们做的和我做的完全一样。
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
}
我在 Startup.cs
中这样配置 ApplicationUser
:
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>();
我的 _LoginPartial.cshtml 看起来像:
@inject SignInManager<ApplicationUser> signInManager
@if (signInManager.IsSignedIn(User))
{
<li class="nav-item">
<form method="post" asp-controller="account" asp-action="logout">
<button type="submit" style="width:auto"
class="nav-link btn btn-link py-0">
Logout @User.Identity.Name
</button>
</form>
</li>
}
else
{
<li class="nav-item">
<a class="nav-link" asp-controller="account" asp-action="index">
Login
</a>
</li>
}
我在 _ViewImports.cshtml
视图中导入了 Microsoft.AspNetCore.Identity
和 OAuth2Demo.Models
。
当我注入 IdentityUser
而不是 ApplicationUser
时,我可以完美地工作。我可以使用 ApplicationUser 执行其他操作,例如将用户存储在数据库中,为其他目的获取声明。
the type of namespace ApplicationUser can not be found(are you missing a using directive or an assembly reference).
I've imported Microsoft.AspNetCore.Identity and OAuth2Demo.Models in _ViewImports.cshtml view
要解决此问题,请检查您的 ApplicationUser
class 是否定义如下。
namespace OAuth2Demo.Models
{
public class ApplicationUser: IdentityUser
{
//...
//properties here
//...
请注意,_ViewImports.cshtml
文件可以放在任何文件夹中,在这种情况下,它只会应用于该文件夹中的页面或视图及其子文件夹。因此,请确保您在正确的 _ViewImports.cshtml
文件中添加 @using OAuth2Demo.Models
。
此外,您可以尝试在_signinPartial.cshtml
中指定一个完全限定的名称,然后检查它是否可以正常工作。
@inject SignInManager<OAuth2Demo.Models.ApplicationUser> signInManager