【发布时间】:2021-01-01 12:47:45
【问题描述】:
我创建了一个扩展 IdentityUser 的 ApplicationUser。现在我想在视图中显示/隐藏登录和注销。因此,我尝试在_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 执行其他操作,例如将用户存储在数据库中,为其他目的获取声明。
【问题讨论】:
-
ApplicationUser是否与_LoginPartial在同一个命名空间中?如果没有,您可能需要为该命名空间添加using语句。 -
我在 _ViewImports.cshtml 中添加了命名空间。
-
您说您添加了另外两个命名空间,但不是
ApplicationUser的一个 - 如果您添加了更多命名空间,请通过将其添加到问题中向我们展示。并且_ViewImports.cshtml包含在_LoginPartial.cshtml中。您遗漏了很多信息,无法全面了解问题所在。
标签: c# asp.net-core claims-based-identity