Update 07082021
This commit is contained in:
81
WebFormApp/Site.Master.cs
Normal file
81
WebFormApp/Site.Master.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Principal;
|
||||
using System.Web;
|
||||
using System.Web.Security;
|
||||
using System.Web.UI;
|
||||
using System.Web.UI.WebControls;
|
||||
using Microsoft.AspNet.Identity;
|
||||
|
||||
namespace WebFormApp
|
||||
{
|
||||
public partial class SiteMaster : MasterPage
|
||||
{
|
||||
private const string AntiXsrfTokenKey = "__AntiXsrfToken";
|
||||
private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
|
||||
private string _antiXsrfTokenValue;
|
||||
|
||||
protected void Page_Init(object sender, EventArgs e)
|
||||
{
|
||||
// Der Code unten schützt vor XSRF-Angriffen.
|
||||
var requestCookie = Request.Cookies[AntiXsrfTokenKey];
|
||||
Guid requestCookieGuidValue;
|
||||
if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
|
||||
{
|
||||
// Das Anti-XSRF-Token aus dem Cookie verwenden
|
||||
_antiXsrfTokenValue = requestCookie.Value;
|
||||
Page.ViewStateUserKey = _antiXsrfTokenValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Neues Anti-XSRF-Token generieren und im Cookie speichern
|
||||
_antiXsrfTokenValue = Guid.NewGuid().ToString("N");
|
||||
Page.ViewStateUserKey = _antiXsrfTokenValue;
|
||||
|
||||
var responseCookie = new HttpCookie(AntiXsrfTokenKey)
|
||||
{
|
||||
HttpOnly = true,
|
||||
Value = _antiXsrfTokenValue
|
||||
};
|
||||
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)
|
||||
{
|
||||
// Anti-XSRF-Token festlegen
|
||||
ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
|
||||
ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Anti-XSRF-Token überprüfen
|
||||
if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
|
||||
|| (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
|
||||
{
|
||||
throw new InvalidOperationException("Fehler bei der Überprüfung des Anti-XSRF-Tokens.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void Page_Load(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected void Unnamed_LoggingOut(object sender, LoginCancelEventArgs e)
|
||||
{
|
||||
Context.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user