i have a login view in asp.net-mvc. i want that user cannot able to enter tag in username or password field
-
Why? What kind of tags? My password can be <password>...Bojan Milenkoski– Bojan Milenkoski2010-06-01 14:02:52 +00:00Commented Jun 1, 2010 at 14:02
-
thats why i want any solution in which user can input any character, but default if i enter <scipt> tag in username it just blast and yellow screen comesFraz Sundal– Fraz Sundal2010-06-01 14:05:28 +00:00Commented Jun 1, 2010 at 14:05
-
Fraz your questions says the opposite, you should correct the question title and text, maybe changing prevent for allow, and user cannot for the users areGabriel Guimarães– Gabriel Guimarães2010-06-01 19:30:43 +00:00Commented Jun 1, 2010 at 19:30
2 Answers
Why would you want preventing the user from entering tags? Leave him enter whatever he wants. Why preventing someone from having a password such as <script>alert('hello');</script> - it looks like a pretty strong password. Personally I hate web sites limiting my choices for a password.
Just make sure that you encode everything you are outputting inside the views:
So instead of:
<div>Hello <%= Model.Username %></div>
Always use:
<div>Hello <%= Html.Encode(Model.Username) %></div>
or:
<div>Hello <%: Model.Username %></div>
if you are working with ASP.NET 4.0
Also, as pointed out by @Jab in the comments section, in order to accept such input from the user you might need to decorate the controller action that will handle the submission with the [ValidateInput(false)] attribute.
4 Comments
Is there some reason regular sanitisation approaches As described here will not work with MVC?