Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autocomplete=off not working in mvc 5

I am trying not to generate autocomplete in browser for that I am trying this

@Html.TextBoxFor(
model => model.UserName, 
new { @class = "form-control", autocomplete = "off" })
@Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })

which generate HTML like this

<input type="text" value="" name="UserName" id="UserName" data-val-required="User name is required." data-val="true" class="form-control" autocomplete="off">

In other views I have use the same and it is working fine. but in login view autocomplete="off" not working. for doing so I have clear model state like this in controller

 ModelState.Remove("UserName");

reference taken from the following sites

  • How to Turn Off Form Autocompletion
  • Disable autocomplete on html helper textbox in MVC
  • How to disable autocomplete in MVC Html Helper
  • Remove browser autocompletion in MVC

I still wonder what is wrong in my code? Your suggestion means a lot to me.

like image 739
Iswar Avatar asked Sep 29 '22 20:09

Iswar


1 Answers

As per Bharat's answer it actually, the issue is not of the autocomplete, the issue comes from the browser's saved logins.

but we can overcome this without going to clear saved passwords of the browser. As per my intention, all browsers doing similar duties that the field before the password is assumed as Username and it uses the last logged in data will be pasted automatically even if we do autocomplete="off".

The solution is to make the password's field autocomplete="new-password", but this will work only in HTML5 or above supported browsers only...

like image 93
Luqman Avatar answered Oct 03 '22 09:10

Luqman