Skip to main content

CSRF Asp.Net

Asp.Net CSRF 

Image result for CSRF

Step 1: Add this code in master page, if there is no master page than add it in page.
//This is code is to generate the random token
protected void Page_Init(Object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                var pageName = Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath);
                var pageToken = pageName + "_ID";
                RandomNumberGenerator rng = new RNGCryptoServiceProvider();
                var tokenData = new byte[32];
                rng.GetBytes(tokenData);
                var token = Convert.ToBase64String(tokenData);
                Session["token"] = token.Trim();
                CSRFToken.Value = pageToken.Trim() + token.Trim();
            }
        }
Step:2 Verify the token on each post(Add,Update,Delete button event)
protected void btnAddUpdate_Click(object sender, EventArgs e)
        {
            var pageToken = Path.GetFileName(HttpContext.Current.Request.Url.AbsolutePath) + "_ID";
            pageToken = pageToken + Session["token"];
            if (CSRFToken.Value.Trim() != pageToken.Trim())
            {
                Session.Abandon();
                Session.Clear();
                Response.Redirect("Error.aspx");
                //LogOut of the Application
            }
            else
            {
Your Code
    }
}

Comments

Popular posts from this blog

Visual Studio 2010 Error HRESULT E_FAIL has been returned from a call to a COM component.

I was using Visual Studio 2010.  I was debugging a web application and an exception happened and VS 2010 froze.  I ended the VS 2010 in the task manager and when I went back to developing, I found on every form for every ASP.net control I get:  Error Creating Control - Error HRESULT E_FAIL has been returned from a call to a COM component.  Also I am unable to edit  the form or add anything from the toolbox. Solution: This error comes because of Caching of Visual Studio Delete the Cache. You can delete the project cache at "Program Files\Microsoft Visual Studio 10.0\Common7\IDE\ProjectTemplatesCache", then run "devenv /setup" to build the cache again to see if it helps.

Password Protected File Validation for(.doc/.docx/.xls/.xlsx/.pdf) file types

Password Protected File Validation for(.doc/.docx/.xls/.xlsx/.pdf) file types protected void btnUpload_Click( object sender, EventArgs e)         {             //Check if File Upload control has file or not             if (FileUpload1.HasFile)             {                 //Get Uploaded file bytes                 var bytes = FileUpload1.FileBytes;                 //Get Uploaded File Extension                 FileInfo objFileInfo = new FileInfo (FileUpload1.FileNam...

PDF Editor

http://www.pdfescape.com PDFescape is a free, online PDF reader, editor, form filler, & form designer. Free PDF Reader Free PDF Editor Free PDF Form Filler Free PDF Form Designer Free Webmaster Tools No Downloads No Watermarks A new way to open and edit PDF files online, PDFescape frees users from the typical software requirements for using the de facto document file format. Completely online, PDFescape requires no more than a modern internet browser and an active internet connection. Select any of the major features above to learn more.