简体   繁体   English

WebMatrix.WebData.WebSecurity - 如何只通过PasswordResetToken获取UserName

[英]WebMatrix.WebData.WebSecurity - How can I get UserName by only having PasswordResetToken

I just wanted to ask for help to get my scenario work? 我只是想请求帮助以使我的方案工作? I want to get the UserName using a PasswordResetToken. 我想使用PasswordResetToken获取UserName。

This is my scenario. 这是我的情景。

  1. I have a forgot password feature in my website that would send a passwordresettoken email a change password to the user. 我的网站上有一个忘记密码的功能,它会向用户发送一个密码重置电子邮件密码。
  2. I wanted to send just the passwordresettoken string only. 我只想发送passwordresettoken字符串。
  3. When the user clicks the link. 当用户单击链接时。 I will just query the request["token"] to get the username and and then will allow the user to change password and autologin. 我将只查询请求[“token”]以获取用户名,然后将允许用户更改密码和自动登录。

    this is my code below: 这是我的代码如下:

     public ActionResult ChangePassword() { ChangePasswordModel model = new ChangePasswordModel(); string token=string.Empty; try { token = Request["token"].ToString(); int userId = WebSecurity.GetUserIdFromPasswordResetToken(token); if (userId > 0) { //Get the user object by (userid) //??????????????????? //??????????????????? } else { throw new Exception("The change password token has expired. Please go to login page and click forgot password again."); } } catch { model.HasError = true; ModelState.AddModelError("", "The change password token has expired. Please go to login page and click forgot password again."); } return View(model); } 

Thank you in advance. 先感谢您。

Look at the remark at the end of this article: WebSecurity.GeneratePasswordResetToken Method . 请看本文末尾的注释: WebSecurity.GeneratePasswordResetToken方法

I'll copy the relevant part for your convenience: 为方便起见,我会复制相关部分:

If users have forgotten their password, they can request a new one. 如果用户忘记了密码,他们可以申请新密码。 To provide a new password, do the following: 要提供新密码,请执行以下操作:

  1. Create a password-reset page that has a field where users can enter their email address. 创建一个密码重置页面,其中包含一个用户可以输入其电子邮件地址的字段。
  2. When a user has entered his or her email address in the password-reset page, verify that the email address represents a valid user. 当用户在密码重置页面中输入了他或她的电子邮件地址时,请验证该电子邮件地址是否代表有效用户。 If it does, generate a password reset token by calling the GeneratePasswordResetToken(String, Int32) method. 如果是,则通过调用GeneratePasswordResetToken(String,Int32)方法生成密码重置令牌。
  3. Create a hyperlink that points to a confirmation page in your site and that includes the token as a query-string parameter in the link's URL. 创建指向站点中的确认页面的超链接,该超链接将令牌作为查询字符串参数包含在链接的URL中。
  4. Send the link to a user in an email message. 通过电子邮件将链接发送给用户。 When the user receives the email message, he or she can click the link to invoke the confirmation page. 当用户收到电子邮件消息时,他或她可以单击该链接以调用确认页面。
  5. Create a confirmation page that extracts the token from the URL parameter and that lets the user enter a new password. 创建一个确认页面,该页面从URL参数中提取令牌,并允许用户输入新密码。
  6. When the user submits the new password, call the ResetPassword(String, String) method and pass the password reset token and the new password. 当用户提交新密码时,请调用ResetPassword(String,String)方法并传递密码重置令牌和新密码。 If the token is valid, the password will be reset. 如果令牌有效,密码将被重置。 If the token is not valid (for example, it has expired), display an error message. 如果令牌无效(例如,它已过期),则显示错误消息。

Highlighting is mine. 突出显示是我的。 Basically you do not need the user name. 基本上你不需要用户名。 The framework does all the heavy lifting for you. 该框架为您完成了所有繁重的工作。

Addressing your comment, I would not recommend automatically logging the user in. It's a good practice for them to log manually to check that this password changing thingie has actually worked, and not to discover that it did not only next time around. 解决你的评论,我不建议自动登录用户。这是一个很好的做法,他们手动记录检查这个密码更改的东西实际上是否有效,而不是发现它不仅仅是下一次。

Anyway, you can do this: 无论如何,你可以这样做:

SimpleMembershipProvider provider = (SimpleMembershipProvider)Membership.Provider;
string username = provider.GetUserNameFromId(userId);

Reference: GetUserNameFromId . 参考: GetUserNameFromId

I think the WebSecurity.GetUserIdFromPasswordResetToken(string token) method do what you want. 我认为WebSecurity.GetUserIdFromPasswordResetToken(string token)方法WebSecurity.GetUserIdFromPasswordResetToken(string token)你的需要。

More info here . 更多信息在这里

Update : 更新

Sorry but I didn't saw that you were already using that method... So if you want get the username and you are using code first migrations of Entity Framework, you can get the username with the following LINQ expression: 抱歉,我没有看到您已经在使用该方法...因此,如果您想获取用户名并且您正在使用代码首次迁移Entity Framework,您可以使用以下LINQ表达式获取用户名:

string username = yourDbContext.UserProfiles.FirstOrDefault(up=>up.UserId == userId).Username;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在WPF应用程序中使用WebMatrix.WebData.WebSecurity - Using WebMatrix.WebData.WebSecurity in a WPF application 使用MySql的Webmatrix.WebData.WebSecurity无法正常工作。 WebSecurity.UserExists()检查中引发SQL语法错误 - Webmatrix.WebData.WebSecurity with MySql is not working. Throwing sql syntax error in WebSecurity.UserExists() check 无法获取MVC5中引用的用于简单成员资格的Webmatrix.WebData? - Can't get Webmatrix.WebData for Simple Membership referenced in MVC5? WebMatrix WebData InitializeDatabaseConnection - WebMatrix WebData InitializeDatabaseConnection WebMatrix WebSecurity PasswordSalt - WebMatrix WebSecurity PasswordSalt WebMatrix中的WebSecurity的PasswordReset无法正常工作 - PasswordReset with WebSecurity in WebMatrix not working 我尝试使用WebMatrix.WebData,但即使卸载了该包,也一直收到此错误 - I tried to use WebMatrix.WebData, but keep getting this error, even after I uninstalled the package WebMatrix.WebData无法从单独的ASP.NET应用程序中读取成员资格信息 - WebMatrix.WebData can't read membership information from a separate ASP.NET application Webmatrix.WebData登录方法导致异常 - Webmatrix.WebData Login method results in exception 当 AD 不可用时,如何从用户名中获取用户的 SID? - How can I get the SID of a user having from his/her username when the AD is not available?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM