![](/img/trans.png)
[英]How do I get the user details straight after login using Identity on ASP.NET Core?
[英]How do I get the UserId at login using asp.net Identity?
我想獲取成功登錄的用戶的UserId,以便可以將其寫入帶有時間戳的數據庫中以進行審核。 這是Login cs文件中的代碼片段
if (Page.IsValid)
{
// Validate the user password
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
var result = signinManager.PasswordSignIn((this.LoginView1.FindControl("username") as TextBox).Text, (this.LoginView1.FindControl("password") as TextBox).Text, (this.LoginView1.FindControl("RememberMe") as CheckBox).Checked, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
break;
我嘗試添加
string thisuser = User.Identity.GetUserId().ToString();
並且
string thisuser = System.Web.HttpContext.Current.User.Identity.GetUserId().ToString;
在重定向之前,但在每種情況下它都返回一個空值。 如果不在此Login方法中,我實際上可以從哪里獲得用戶ID?
您始終可以按名稱查找用戶。 由於登錄成功,因此使用用戶名沒有問題。
var userName = ((this.LoginView1.FindControl("username") as TextBox).Text;
//...
switch (result)
{
case SignInStatus.Success:
var loggedInUser = manager.FindByName(userName);
//Do your log with loggedInUser!
IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
break;
//...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.