Asp.net mvc 用户登录验证后为空
已使用Identity Framework创建ASP.Net应用程序。用户身份验证成功后,Asp.net mvc 用户登录验证后为空,asp.net-mvc,asp.net-web-api,asp.net-identity,Asp.net Mvc,Asp.net Web Api,Asp.net Identity,已使用Identity Framework创建ASP.Net应用程序。用户身份验证成功后,System.Web.HttpContext.Current.user.Identity为null且System.Web.HttpContext.Current.user.Identity.IsAuthenticated为false public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
System.Web.HttpContext.Current.user.Identity
为null
且System.Web.HttpContext.Current.user.Identity.IsAuthenticated
为false
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
if (!ModelState.IsValid)
{
return View(model);
}
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
UserManager.AddClaim(User.Identity.GetUserId(),new Claim(ClaimTypes.Role,"Admin"));
公共异步任务登录(LoginViewModel模型,字符串返回URL)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
var result=wait-SignInManager.PasswordSignInAsync(model.Email、model.Password、model.RememberMe、shouldllockout:true);
开关(结果)
{
案例标志状态成功:
ApplicationUser user=System.Web.HttpContext.Current.GetOwinContext().GetUserManager().FindById(System.Web.HttpContext.Current.user.Identity.GetUserId());
UserManager.AddClaim(User.Identity.GetUserId(),新声明(ClaimTypes.Role,“Admin”);
身份验证后,我需要向经过身份验证的用户添加一些角色。在第一次重定向发生后,您将在
user.Identity.GetUserId()中获得值。一旦重定向发生,它将创建cookie,然后您使用user.Identity.GetUserId()
或User.Identity.IsAuthenticated
您将在User.Identity.GetUserId()
中获得值,在第一次重定向后发生。一旦重定向,它将创建cookie,然后您在什么位置使用User.Identity.GetUserId()
或User.Identity.IsAuthenticated
(函数/类)我可以将角色分配给经过身份验证的用户吗?@Sami:为什么在登录后分配角色。它应该存储在Db中。在哪个位置(函数/类)我可以为经过身份验证的用户分配角色吗?@Sami:为什么在登录后分配角色。它应该存储在Db中。我们只能在第一次重定向后获取用户Id我们只能在第一次重定向后获取用户Id