Asp.net 什么设置';是否已验证User.Identity.Name和User.Identity.i?
我想知道用户身份名的设置,并将Asp.net 什么设置';是否已验证User.Identity.Name和User.Identity.i?,asp.net,asp.net-mvc,asp.net-identity,iprincipal,iidentity,Asp.net,Asp.net Mvc,Asp.net Identity,Iprincipal,Iidentity,我想知道用户身份名的设置,并将isAuthenticated更改为true 为什么User.Identity.Name是空字符串,而User.Identity.IsAuthenticatedfalse在SignInManager.PasswordSignInAsync返回Success之后 // POST: /Account/Login [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<Acti
isAuthenticated
更改为true
为什么User.Identity.Name
是空字符串,而User.Identity.IsAuthenticated
false
在SignInManager.PasswordSignInAsync
返回Success
之后
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var userIdentityNameTest = User.Identity.Name; // Empty string
var result = await SignInManager.PasswordSignInAsync(
model.Email, model.Password,
model.RememberMe, shouldLockout: false);
// result is "Success"
userIdentityNameTest = User.Identity.Name;
// userIdentityNameTest is still an empty string?
// User.Identity.IsAuthenticated is still false?
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case SignInStatus.LockedOut:
return View("Lockout");
case SignInStatus.RequiresVerification:
return RedirectToAction("SendCode", new { ReturnUrl = returnUrl,
RememberMe = model.RememberMe });
case SignInStatus.Failure:
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
//POST:/Account/Login
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务登录(LoginView模型,字符串返回URL)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
var userIdentityNameTest=User.Identity.Name;//空字符串
var result=await-SignInManager.PasswordSignInAsync(
型号。电子邮件,型号。密码,
model.RememberMe,shoulldlockout:false);
//结果是“成功”
userIdentityNameTest=User.Identity.Name;
//userIdentityNameTest仍然是空字符串吗?
//User.Identity.IsAuthenticated仍然为false?
开关(结果)
{
案例标志状态成功:
返回重定向到本地(returnUrl);
案例标志状态锁定输出:
返回视图(“锁定”);
案例标志状态。要求验证:
return RedirectToAction(“SendCode”,新的{ReturnUrl=ReturnUrl,
RememberMe=model.RememberMe});
案例信号状态故障:
违约:
AddModelError(“,”登录尝试无效“);
返回视图(模型);
}
}
如果不使用TwoFactorAuthentication
,则似乎SignInManager.PasswordSignInAsync
仅验证输入的数据并运行AuthenticationManager.SignIn
<代码>AuthenticationManager.SignIn在这种情况下,仅将身份验证cookie设置为响应
因此,User.Identity
在应用程序的后续请求中可用。要通过Name
获取ApplicationUser
,可以使用ApplicationUserManager
如下:
UserManager.FindByNameAsync(model.Name)
在执行“SignInManager.PasswordSignInAsync”后,如何获取或将已登录用户的名称设置为“user.Identity”或“HttpContext.user”?