简体   繁体   English

如何使用Cookie记住C#中的用户名和密码

[英]How to remember the username and password in c# using cookie

我是C#的新手,我用两个字段用户名和密码创建了一个登录表单,“用户也记得我”复选框。.首次用户使用“记住我”复选框选中了该表单。用户下次使用C#登录时,从cookie的密码字段中输入密码?

Hope you are using FormsAuthentication. 希望您正在使用FormsAuthentication。 In the SetAuthCookie method, there is one parameter, PersistCookie, set this value to true, ASP.Net runtime will store the user credentials to the client. 在SetAuthCookie方法中,有一个参数PersistCookie将此值设置为true,ASP.Net运行时会将用户凭据存储到客户端。

You can find more details here : How to: Implement Simple Forms Authentication 您可以在此处找到更多详细信息: 如何:实现简单表单身份验证

If I understand your question correctly, what you want is to have the login form prefilled with the login information, the next time the user visits your page. 如果我正确理解了您的问题,那么您想要的是在用户下次访问您的页面时,在登录表单中预先填写登录信息。

You should NOT use a cookie to do that, for security reasons. 应该使用cookie来做到这一点,出于安全原因。 The best way to do it is instead to let the browser store the login information for you. 最好的方法是让浏览器为您存储登录信息。 I bet your browser has asked you "Save this password? Yes, Never, Not now" before? 我敢打赌,您的浏览器之前曾问过您“保存此密码?是,从不,不是现在”吗?

To accomplish that, the browser needs to recognize that it is dealing with a login form. 为此,浏览器需要识别它正在处理登录表单。 I believe there might be some differences between various browsers how they detect that, but following these steps should be sufficient for most browsers I belive: 我相信各种浏览器在检测到它们的方式上可能会有一些差异,但是遵循这些步骤对于我相信的大多数浏览器应该足够了:

  1. Make sure the input fields are named username and password 确保输入字段被命名为usernamepassword
  2. Make sure that the password input is of type password 确保密码输入的类型为password
  3. Make sure that the form has a proper action attribute 确保表单具有正确的操作属性
  4. Make sure that the form has a submit button 确保表单上有一个提交按钮

Asp.net has an implementation of forms authentication ready to use. Asp.net具有可立即使用的表单身份验证实现。

Use the methods FormsAuthentication.SignIn() and FormsAuthentication.SignOut() to create the auth cookie or delete it. 使用FormsAuthentication.SignIn()和FormsAuthentication.SignOut()方法创建或删除auth cookie。

For more info: http://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication.aspx 有关更多信息: http : //msdn.microsoft.com/zh-cn/library/system.web.security.formsauthentication.aspx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM