简体   繁体   English

如何设置正确的用户名和密码文本框?

[英]How to set the correct username and password textboxes?

I have a login screen with a user name and password but it also has a company field which is kind of like having a domain. 我有一个带有用户名和密码的登录屏幕,但它也有一个公司字段,有点像有域名。

The problem is that the browsers are using the domain box like the username so when you save the password in the browser, if you type the domain it fills the password in the password box, then all you need to to is add the username which is most likely that computer user. 问题是浏览器正在使用域名框这样的用户名,因此当您在浏览器中保存密码时,如果您键入域,它会在密码框中填写密码,那么您需要添加的用户名是最有可能的是计算机用户。 It is obviously a serious security problem. 这显然是一个严重的安全问题。

Eg User: Tom, Domain: Netweb, Pass: test 例如用户:Tom,Domain:Netweb,Pass:test

Tom logs in once and clicks to save his password. Tom登录一次并单击以保存其密码。 The next time he comes back, he enters Netweb into the domain and presses return, it fills the password which was saved for that domain and then he can enter his username afterwards. 下次他回来时,他将Netweb输入域名并按回车,它会填写为该域名保存的密码,然后他可以输入他的用户名。

What can I do about this? 我该怎么办? Is there a way to set the username so that it doesn't use the company or a way to use the top two before adding the password? 有没有办法设置用户名,以便在添加密码之前不使用公司或使用前两种方法?

用户名问题的示例

My code is below: 我的代码如下:

<tr class="center">
    <td class="center">User Name
        <br />
        <asp:TextBox ID="txtUser" runat="server"></asp:TextBox>
    </td>
</tr>
<tr class="center">
    <td class="center">Company
        <br />
        <asp:TextBox ID="txtCompany" runat="server"></asp:TextBox>
    </td>
</tr>
<tr class="center">
    <td class="center">Password
        <br />
        <asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox>
        <br />Remember me?
        <asp:CheckBox ID="chkPersistCookie" runat="server" AutoPostBack="false" />
        <br />
        <br />
        <asp:Button ID="btnSubmit" runat="server" Text="Login" CssClass="center" OnClick="btnSubmit_Click" />
        <br />
        <asp:Label ID="lblMessage" runat="server"></asp:Label>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUser" ErrorMessage="Please enter a user name" ForeColor="Red"></asp:RequiredFieldValidator>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtCompany" ErrorMessage="Please enter a company" ForeColor="Red"></asp:RequiredFieldValidator>
        <br />
        <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtPass" ErrorMessage="Please enter a password" ForeColor="Red"></asp:RequiredFieldValidator>
    </td>
</tr>

What you're facing here is known as autocomplete attribute for Form Values. 您在此处面临的内容称为表单值的autocomplete属性。 When you submit a form, Browser saves the form values for further usage on the very same page. 提交表单时,浏览器会保存表单值以便在同一页面上进一步使用。 Browser sometimes also provides the user ability to Save the Password for the very website. 浏览器有时还为用户提供了为网站保存密码的功能。

It is something like this 就是这样的

<input type="text" name="someInputName" autocomplete="off|on" />

But remember, even if the browser saves the data for the autocomplete. 但请记住,即使浏览器保存了自动完成的数据。 It will never ever save the Passwords of the user for the autocomplete feature. 永远不会为自动完成功能保存用户的密码。 They're not saved anywhere until the user allows the software to do so. 在用户允许软件执行此操作之前,它们不会保存在任何位置。

What you're facing here is the Form Autocomplete feature by Browsers. 您在这里面临的是浏览器的表单自动填充功能。 In this case, Browser saves the User's data and then you can just either remove that Data from the Browser by going to the Settings of the browser and further more under the hood, and there selecting the Saved passwords, and removing the password for your site. 在这种情况下,浏览器会保存用户的数据,然后您可以通过转到浏览器的设置以及更多内容从浏览器中删除该数据,然后选择已保存的密码,并删除您网站的密码。

Otherwise, you have no control in preventing what a user want to do. 否则,您无法控制用户想要执行的操作。 But, as Google does. 但是,就像谷歌一样。 You can implement their idea of the Security. 您可以实现他们的安全理念。

What they do is that they show you an input box, of Password type and then they write the Email address that is associated with the account. 他们所做的是他们向您显示密码类型的输入框,然后他们编写与该帐户关联的电子邮件地址。 This way, you will trick the Browser and the browser would think that you require something else and not the password for him. 这样,你会欺骗浏览器,浏览器会认为你需要别的东西,而不是他的密码。

There are some other things that you can do too. 您还可以做其他一些事情。 Like, getting the user's Email address on one page, and then getting Password on the next page—like Google does now. 比如,在一个页面上获取用户的电子邮件地址,然后在下一页上获取密码 - 就像Google现在所做的那样。

如果问题只是在文本框中自动提示密码,则需要禁用文本框的自动完成属性

With the autocomplete field as it is, you're best off making as many helpful pointers to the browser as possible. 使用自动完成字段,您最好尽可能多地为浏览器提供有用的指针。 In your case, that would be something like: 在你的情况下,这将是这样的:

<asp:TextBox ID="username" x-autocompletetype="given-name" runat="server"></asp:TextBox>
<asp:TextBox ID="txtCompany" x-autocompletetype="organization" runat="server"></asp:TextBox>
<asp:TextBox ID="password" x-autocompletetype="password" runat="server" TextMode="Password"></asp:TextBox>

Note that this is meant for forms proper, not the login types that you refer to. 请注意,这适用于正确的表单,而不是您引用的登录类型。 Also, these are tested with HTML Form Inputs - I assume ASP should transfer custom attributes into the resulting HTML code, but it's a possible fail. 此外,这些是使用HTML表单输入测试的 - 我假设ASP应该将自定义属性传输到生成的HTML代码中,但它可能会失败。 The ASP ID maps to HTML Input name for sure though. ASP ID确实映射到HTML输入name


This was a very interesting question for me, because Autocomplete seems to be one of those things that's not even close to a standard yet. 这对我来说是一个非常有趣的问题,因为自动完成似乎是那些甚至还没有接近标准的事情之一。

HTML5 W3 Spec HTML5 W3 Spec

The "on" keyword indicates that the user agent is allowed to provide the user with autocompletion values, but does not provide any further information about what kind of data the user might be expected to enter. “on”关键字表示允许用户代理向用户提供自动完成值,但不提供有关用户可能要输入的数据类型的任何进一步信息。 User agents would have to use heuristics to decide what autocompletion values to suggest. 用户代理必须使用启发式方法来确定要建议的自动完成值。

A team from Google seems to want to change that with their proposal Google的一个团队似乎希望通过他们的提案来改变这一点

Current autofill products rely on contextual clues to determine the type of data that should be filled into form elements. 当前的自动填充产品依赖于上下文线索来确定应填充到表单元素中的数据类型。 Examples of these contextual clues include the name of the input element, the text surrounding it, and placeholder text. 这些上下文线索的示例包括input元素的名称,它周围的文本和占位符文本。

We have discussed the shortcomings of these ad hoc approaches with developers of several autofill products, and all have been interested in a solution that would let website authors classify their form fields themselves. 我们已经与几个自动填充产品的开发人员讨论了这些特殊方法的缺点,并且所有人都对一个允许网站作者自己对其表单字段进行分类的解决方案感兴趣。 While current methods of field classification work in general, for many cases they are unreliable or ambiguous due to the many variations and conventions used by web developers when creating their forms: 虽然当前的字段分类方法通常起作用,但在许多情况下,由于Web开发人员在创建表单时使用了许多变体和约定,因此它们不可靠或不明确:

Here's a nice link that covers how this proposal, if implemented, will work: 这是一个很好的链接 ,涵盖了此提案如果实施,将如何工作:

<input type="text" name="firstname" value="" x-autocompletetype="given-name">  
<input type="text" name="doesn-matter" value="" x-autocompletetype="family-name">  
<input id="email" type="text" name="email" value="" x-autocompletetype="email">

Further links: 更多链接:

最后通过交换文本框解决了这个问题。

How we handle this is by appending a random value onto the id and name attributes of the textbox on every page load. 我们如何处理这个问题是在每个页面加载时将随机值附加到文本框的id和name属性上。 Then the browser has no way to recall the auto complete, since it was stored for a field with different identifiers. 然后浏览器无法调用自动完成,因为它是为具有不同标识符的字段存储的。

Use ClientIDMode="Static" for this, so that .net doesn't generate the Id for you. 为此使用ClientIDMode =“Static”,以便.net不会为您生成Id。

I am not sure I understood your question. 我不确定我理解你的问题。 But a couple of tips: 但有几个提示:

You can tell Chrome not to remember passwords using autocomplete: http://www.w3schools.com/tags/att_input_autocomplete.asp 您可以告诉Chrome不要使用自动填充功能记住密码: http//www.w3schools.com/tags/att_input_autocomplete.asp

You can change the tab sequence of the textboxes using tabindex: http://www.w3schools.com/tags/att_global_tabindex.asp 您可以使用tabindex更改文本框的选项卡顺序: http//www.w3schools.com/tags/att_global_tabindex.asp

如果你唯一的问题是你可以自动填写密码,你可以通过代码隐藏来执行以下操作来禁用它:

txtPass.Attributes.Add("autocomplete", "off");

Use either for Domain/Company: 使用域名/公司:

<asp:TextBox id="Textbox1" runat="server" autocomplete="off"></asp:TextBox>

Or from the CodeBehind: 或者从CodeBehind:

Textbox1.Attributes.Add("autocomplete", "off");

It will disable autocomplete for textbox company. 它将禁用textbox公司的自动完成功能。

If it doesn't resolve the problem, the alternate way is to use some validation like: 如果它不能解决问题,另一种方法是使用一些验证,如:

  1. When Domain/Company is entered, checks if Username is provided. 输入域/公司时,检查是否提供了用户名。

  2. If username is not provided & password is present, give error message to fill username first & deliberately empty textboxes for Company & password. 如果未提供用户名并且密码存在,请提供错误消息以首先填写用户名并故意清空公司和密码的文本框。

Some code in jquery will be like: jquery中的一些代码如下:

$(txtDomain).focusout(function() {
      if($txtUserName).val().trim()=="" && $(txtDomain).val().trim()!="" && $(txtPass).val().trim()!="")
      {
         alert('Please fill the username first');
         $(txtPass).val('');
         $(txtDomain).val('');
      }

I know this is not good solution & also has usability issues. 我知道这不是一个好的解决方案,也有可用性问题。 But this can be last resort, if anything else not works. 但这可以是最后的手段,如果其他任何不起作用。

Hope it helps. 希望能帮助到你。

Browser auto fills credentials to wrong text field? 浏览器自动将凭据填充到错误的文本字段?

Sometimes I notice this strange behavior on Chrome and Safari, when there are password fields in the same form. 当有相同表格的密码字段时,有时我会注意到Chrome和Safari上的这种奇怪行为 I guess, the browser looks for a password field to insert your saved credentials. 我猜,浏览器会查找密码字段以插入您保存的凭据。 Then it autofills (just guessing due to observation) the nearest textlike-input field, that appears prior the password field in DOM. 然后它自动填充(只是猜测由于观察)最近的textlike-input字段,它出现在DOM中的密码字段之前。 As the browser is the last instance and you can not control it, sometimes even autocomplete=off would not prevent to fill in credentials . 由于浏览器是最后一个实例而您无法控制它,因此有时甚至autocomplete = off也不会阻止填写凭据 This readonly-fix worked for me. 这个readonly-fix对我有用。

As you swapped the order to " user ... password ... company" it works. 当您将订单交换为“用户...密码...公司”时,它可以正常工作。 Again the Browser will use the field prior to password, now user, to fill in the credentials. 浏览器将再次使用密码(现在是用户)之前的字段来填写凭据。

fix browser autofill in: set readonly and set writeble on focus (click and tab) 修复浏览器自动填充:set readonly并在焦点上设置writeble(单击和制表符)

 <input type="password" readonly  
     onfocus="$(this).removeAttr('readonly');"/>

Manage the cookie manually, This is the jQuery Sample. 手动管理cookie,这是jQuery示例。

     $("#chkPersistCookie").change(function() {

        if(this.checked) {

        var username = $('#txtUser').attr("value");
        var password = $('#txtPass').attr("value");

        $.cookie('username', username, { expires: 14 });
        $.cookie('password', password, { expires: 14 });
        $.cookie('remember', true, { expires: 14 });

    } else {
        $.cookie('username', null);
        $.cookie('password', null);
        $.cookie('remember', null);

    }
    });

To get the cookie and assign it back to the control use the following 要获取cookie并将其分配回控件,请使用以下命令

var remember = $.cookie('remember');
    if ( remember == 'true' ) {

        $("#chkPersistCookie").attr("checked",true)
        var username = $.cookie('username');
        var password = $.cookie('password');

        $('#txtUser').attr("value", username);
        $('#txtPass').attr("value", password);
    }

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

相关问题 浏览器还记得将密码设置的用户名设置为其他文本框 - Browser Remember password set username to other textboxes also 如何在MS Access中设置用户名和密码 - how to set username and password in ms access 如何在IE中以编程方式设置代理用户名和密码? - How to programmatically set the proxy username and password in IE? 如何在.NET中设置SmtpClient object的用户名和密码? - How to set username and password for SmtpClient object in .NET? 如何更改仅适用于正确用户的存储在文本文件中的用户名和密码 - how to change a username and password stored in a text file only for the correct user 如何在连接字符串中设置用户名和密码以连接数据库? - How to set username and password in connection string for connecting to database? 如何使用配置在 WCF 客户端上设置用户名/密码? - How do you set a username / password on a WCF Client using configuration? “用户名或密码不正确” - 收到相同的消息 - "Username or Password is not correct" - getting the same message 如何检查用户输入的用户名密码和数据库名称是否正确? - How can I check if user enter correct username password and database name? 由于用户不存在/密码/用户名正确而导致尝试登录网站失败时如何返回消息? - How to return a message when a try to login to the website failed, because the user does not exist/password/username arent correct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM