Как проверить net веб-страницу без MVC для CSRF-атаки - PullRequest
0 голосов
/ 03 апреля 2020

Может кто-нибудь подсказать, как проверить приведенный ниже код.

{
    private const string AntiXsrfTokenKey = "__AntiXsrfToken";

    private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";

    private string _antiXsrfTokenValue;

    protected void Page_Init(object sender, EventArgs e)
    {
        // The code below helps to protect against XSRF attacks
        var requestCookie = Request.Cookies[AntiXsrfTokenKey];
        Guid requestCookieGuidValue;
        if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
        {
            // Use the Anti-XSRF token from the cookie
            _antiXsrfTokenValue = requestCookie.Value;
            Page.ViewStateUserKey = _antiXsrfTokenValue;
        }
        else
        {
            // Generate a new Anti-XSRF token and save to the cookie
            _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
            Page.ViewStateUserKey = _antiXsrfTokenValue;

            var responseCookie = new HttpCookie(AntiXsrfTokenKey)
            {
                HttpOnly = true,
                Value = _antiXsrfTokenValue,
                Expires = DateTime.Now.AddMinutes(10.0)
            };
            if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
            {
                responseCookie.Secure = true;
            }
            Response.Cookies.Set(responseCookie);
        }

        Page.PreLoad += master_Page_PreLoad;

    }
    protected void master_Page_PreLoad(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Set Anti-XSRF token
            ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
            ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
        }
        else
        {
            // Validate the Anti-XSRF token
            if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
                || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
            {
                throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
            }
        }
    }

Я написал HTNML-код для изменения данных тестовой веб-страницы, код HTML ниже

<h1>Congratulations! You're a Winner!</h1>
<form action="http://localhost:5717/TONEDEV_Test/Test.aspx" method="post">
    <input type="hidden" name="name" value="Sunnny">
    <input type="hidden" name="mobile" value="03325770815">
    <input type="submit" value="Click to collect your prize!">
</form>

и код тестовой веб-страницы указан ниже

<%@ Page Title="Test" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
<h1>Testing the CSRF Attack</h1>
<div class="row">
<div class="col-md-2">
 Name:
</div>
<div class="col-md-4">
<asp:Label ID="LBName" runat="server"></asp:Label>
</div>
<div class="col-md-2">
Mobile No:
</div>
<div class="col-md-4">
<asp:Label ID="LBMobileNo" runat="server"></asp:Label>
</div>
</div>
</asp:Content>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Test : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        string name = Request["name"];
        string mobNo = Request["mobile"];
        if (name != string.Empty && mobNo != string.Empty)
        {
            LBName.Text = name;
            LBMobileNo.Text = mobNo;
        }
    }
}

Пожалуйста, помогите мне, я использую Visual Studio 2010 и. net 4 framework. Я знаю, что есть улучшения в последней версии, но мне нужна помощь с текущими требованиями. Спасибо

...