User.Identity.IsAuthenticated 无效
User.Identity.IsAuthenticated does not work
问题:当用户通过身份验证时,视图不会更改
我想为登录用户创建一个特殊视图。我现在可以用 User.Identity.IsAuthenticated 到达。我试过了,但是不行。
我实施了注册和身份验证,制作了模型、迁移并创建了数据库。我的身份验证工作正常,因为 cookie 在我的浏览器中创建。所以检查下一个截图和代码:
[ValidateAntiForgeryToken]
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Register(RegisterViewModel model)
{
User user = new User
{
Email = model.Email,
UserName = model.Username
};
if (ModelState.IsValid)
{
var result = await userManager.CreateAsync(user, model.Password);
if(!result.Succeeded)
{
foreach(var error in result.Errors)
{
ModelState.TryAddModelError(error.Code, error.Description);
}
return View(model);
}
}
return RedirectToAction("Index", "Home");
}
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(model.Email, model.Password, true,false);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
}
return View("~Views/Home/Index", model);
}
Cookie 是在身份验证后创建的:
Cookie
_Layout.cshtml:
@if(User.Identity.IsAuthenticated)
{
<div class="right-menu">
<ul>
<li><a href="#">@User.Identity.Name</a></li>
<li><a asp-controller="Account" asp-action="Logout">Logout</a></li>
</ul>
</div>
}
else
{
<div class="right-menu">
<ul>
<li><a asp-controller="Account" asp-action="Login">Login</a></li>
<li><a asp-controller="Account" asp-action="Register">Register</a></li>
</ul>
</div>
}
我找到了答案。 User.Identity.IsAuthenticated 总是错误的。所以从 1 小时谷歌搜索为什么它不起作用我意识到我忘了在 Program.cs
中添加这个
app.UseAuthentication();
问题:当用户通过身份验证时,视图不会更改
我想为登录用户创建一个特殊视图。我现在可以用 User.Identity.IsAuthenticated 到达。我试过了,但是不行。
我实施了注册和身份验证,制作了模型、迁移并创建了数据库。我的身份验证工作正常,因为 cookie 在我的浏览器中创建。所以检查下一个截图和代码:
[ValidateAntiForgeryToken]
[HttpPost]
[AllowAnonymous]
public async Task<IActionResult> Register(RegisterViewModel model)
{
User user = new User
{
Email = model.Email,
UserName = model.Username
};
if (ModelState.IsValid)
{
var result = await userManager.CreateAsync(user, model.Password);
if(!result.Succeeded)
{
foreach(var error in result.Errors)
{
ModelState.TryAddModelError(error.Code, error.Description);
}
return View(model);
}
}
return RedirectToAction("Index", "Home");
}
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<IActionResult> Login(LoginViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
if (ModelState.IsValid)
{
var result = await signInManager.PasswordSignInAsync(model.Email, model.Password, true,false);
if (result.Succeeded)
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError(string.Empty, "Invalid Login Attempt");
}
return View("~Views/Home/Index", model);
}
Cookie 是在身份验证后创建的:
Cookie
_Layout.cshtml:
@if(User.Identity.IsAuthenticated)
{
<div class="right-menu">
<ul>
<li><a href="#">@User.Identity.Name</a></li>
<li><a asp-controller="Account" asp-action="Logout">Logout</a></li>
</ul>
</div>
}
else
{
<div class="right-menu">
<ul>
<li><a asp-controller="Account" asp-action="Login">Login</a></li>
<li><a asp-controller="Account" asp-action="Register">Register</a></li>
</ul>
</div>
}
我找到了答案。 User.Identity.IsAuthenticated 总是错误的。所以从 1 小时谷歌搜索为什么它不起作用我意识到我忘了在 Program.cs
中添加这个app.UseAuthentication();