1
我需要使用控制器中当前登录的用户ID。它总是返回null。我已经登录了,这是我的代码。User.Identity.GetUserId()总是在控制器操作中返回空值
if (string.IsNullOrEmpty(userId)) userId = User.Identity.GetUserId();
我试图将基准
using Microsoft.AspNet.Identity;
我也尝试添加一个要求
public const string UserId = "http://schemas.xmlsoap.org/ws/2014/03/mystuff/claims/UserId";
userIdentity.AddClaim(new Claim(CustomClaimTypes.UserId, Id));
当我尝试检索要求,它给空为好。
(identity as ClaimsIdentity).FirstOrNull(CustomClaimTypes.UserId);
任何帮助,将不胜感激。
附加信息请求中的注释:
这是由aspnetuser提供正常登录。
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: 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);
}
Request.IsAuthenticated返回true。
索赔在这里添加。
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
userIdentity.AddClaim(new Claim(CustomClaimTypes.UserId, Id));
return userIdentity;
}
您如何登录? – trailmax
用登录信息编辑我的问题。 –
如果测试Request.IsAuthenticated是否返回true或false? –