[MVC arrangement] 4.asp.net MVC how to prevent csrf attacks

Source: Internet
Author: User
Tags csrf attack

What is a csrf attack?

Csrf (Cross-Site Request Forgery, also known as "one click attack" or session riding, usually abbreviated as csrf or xsrf, is a type of malicious use of websites. Although it sounds like XSS, it is very different from XSS, and the attack method is almost different. XSS uses trusted users in the site, while csrf uses trusted websites by disguising requests from trusted users. Compared with XSS attacks, csrf attacks are often less popular (so the resources to prevent them are quite scarce) and difficult to prevent. Therefore, XSS attacks are considered to be more dangerous than XSS attacks.

Http://baike.baidu.com/view/1609487.htm

Csrf attack scenarios:

Csrf attacks depend on the following assumptions:

Attackers understand the victim's site.

The attacker's target site has a persistent authorization cookie or the victim has the current session cookie.

The target site does not have a second authorization for user behavior on the website

Asp.net MVC provides the following built-in methods to defend against csrf:

1. Use

<% = Html. antiforgerytoken () %>

For example:

<% Using (HTML. beginform ("login", "admin", forw.hod. post) {%> <% = html. antiforgerytoken () %> <% = html. validationsummary (true, "Logon Failed. Correct the error and try again. ") %> <Div> <fieldset> <legend> account information </legend> <Div class =" editor-label "> <% = html. labelfor (M => M. username) %> </div> <Div class = "editor-field"> <% = html. textboxfor (M => M. username) %> <% = html. validationmessagefor (M => M. username) %> <label id = "usernametip"> </label> </div> <Div class = "editor-label"> <% = html. labelfor (M => M. password) %> </div> <Div class = "editor-field"> <% = html. passwordfor (M => M. password) %> <% = html. validationmessagefor (M => M. password) %> </div> <p> <input type = "Submit" value = "Logon"/> </P> </fieldset> </div> <%} %>

2. d. Use [validateantiforgerytoken] to identify the corresponding action:

 
[Httppost] [validateantiforgerytoken] public actionresult login (USR) {If (modelstate. isvalid) {VAR model = dB. context. single <USR> (P => P. systemuser = true & P. username = USR. username & P. password = USR. password); If (model! = NULL) {authenticate. login (USR. username, USR. role); return redirecttoaction ("userlist", "admin");} else {modelstate. the user name or password provided by addmodelerror ("", "is incorrect. ") ;}} Return view (usr );}

 

In fact, we found that Asp.net MVC helped us a lot.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.