Add form and header name to token set
This commit is contained in:
parent
20140c4c15
commit
48ee352022
|
|
@ -15,28 +15,52 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="requestToken">The token that is supplied in the request.</param>
|
/// <param name="requestToken">The token that is supplied in the request.</param>
|
||||||
/// <param name="cookieToken">The token that is supplied in the request cookie.</param>
|
/// <param name="cookieToken">The token that is supplied in the request cookie.</param>
|
||||||
public AntiforgeryTokenSet(string requestToken, string cookieToken)
|
/// <param name="formFieldName">The name of the form field used for the request token.</param>
|
||||||
|
/// <param name="headerName">The name of the header used for the request token.</param>
|
||||||
|
public AntiforgeryTokenSet(
|
||||||
|
string requestToken,
|
||||||
|
string cookieToken,
|
||||||
|
string formFieldName,
|
||||||
|
string headerName)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(requestToken))
|
if (string.IsNullOrEmpty(requestToken))
|
||||||
{
|
{
|
||||||
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(requestToken));
|
throw new ArgumentException(Resources.ArgumentCannotBeNullOrEmpty, nameof(requestToken));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (formFieldName == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(formFieldName));
|
||||||
|
}
|
||||||
|
|
||||||
RequestToken = requestToken;
|
RequestToken = requestToken;
|
||||||
|
|
||||||
// Cookie Token is allowed to be null in the case when the old cookie is valid
|
// Cookie Token is allowed to be null in the case when the old cookie is valid
|
||||||
// and there is no new cookieToken generated.
|
// and there is no new cookieToken generated.
|
||||||
CookieToken = cookieToken;
|
CookieToken = cookieToken;
|
||||||
|
|
||||||
|
FormFieldName = formFieldName;
|
||||||
|
HeaderName = headerName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The token that is supplied in the request.
|
/// Gets the request token.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string RequestToken { get; private set; }
|
public string RequestToken { get; }
|
||||||
|
|
||||||
/// The cookie token is allowed to be null.
|
/// <summary>
|
||||||
/// This would be the case when the old cookie token is still valid.
|
/// Gets the name of the form field used for the request token.
|
||||||
/// In such cases a call to GetTokens would return a token set with null cookie token.
|
/// </summary>
|
||||||
public string CookieToken { get; private set; }
|
public string FormFieldName { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the name of the header used for the request token.
|
||||||
|
/// </summary>
|
||||||
|
public string HeaderName { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the cookie token.
|
||||||
|
/// </summary>
|
||||||
|
public string CookieToken { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -263,7 +263,9 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
{
|
{
|
||||||
return new AntiforgeryTokenSet(
|
return new AntiforgeryTokenSet(
|
||||||
tokenSet.RequestToken != null ? _tokenSerializer.Serialize(tokenSet.RequestToken) : null,
|
tokenSet.RequestToken != null ? _tokenSerializer.Serialize(tokenSet.RequestToken) : null,
|
||||||
tokenSet.CookieToken != null ? _tokenSerializer.Serialize(tokenSet.CookieToken) : null);
|
tokenSet.CookieToken != null ? _tokenSerializer.Serialize(tokenSet.CookieToken) : null,
|
||||||
|
_options.FormFieldName,
|
||||||
|
_options.HeaderName);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class AntiforgeryTokenSetInternal
|
private class AntiforgeryTokenSetInternal
|
||||||
|
|
|
||||||
|
|
@ -108,7 +108,7 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new AntiforgeryTokenSet(requestToken, requestCookie);
|
return new AntiforgeryTokenSet(requestToken, requestCookie, _options.FormFieldName, _options.HeaderName);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SaveCookieToken(HttpContext httpContext, AntiforgeryToken token)
|
public void SaveCookieToken(HttpContext httpContext, AntiforgeryToken token)
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,6 @@ using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Html;
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Http.Internal;
|
using Microsoft.AspNetCore.Http.Internal;
|
||||||
using Microsoft.AspNetCore.Testing;
|
using Microsoft.AspNetCore.Testing;
|
||||||
|
|
@ -75,9 +74,11 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
|
|
||||||
var antiforgery = GetAntiforgery(options);
|
var antiforgery = GetAntiforgery(options);
|
||||||
|
|
||||||
|
var tokenSet = new AntiforgeryTokenSet("hello", "world", "form", "header");
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
var exception = Assert.Throws<InvalidOperationException>(
|
var exception = Assert.Throws<InvalidOperationException>(
|
||||||
() => antiforgery.ValidateTokens(httpContext, new AntiforgeryTokenSet("hello", "world")));
|
() => antiforgery.ValidateTokens(httpContext, tokenSet));
|
||||||
Assert.Equal(
|
Assert.Equal(
|
||||||
@"The antiforgery system has the configuration value AntiforgeryOptions.RequireSsl = true, " +
|
@"The antiforgery system has the configuration value AntiforgeryOptions.RequireSsl = true, " +
|
||||||
"but the current request is not an SSL request.",
|
"but the current request is not an SSL request.",
|
||||||
|
|
@ -431,11 +432,13 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
context.TokenSerializer.Object,
|
context.TokenSerializer.Object,
|
||||||
tokenStore: null);
|
tokenStore: null);
|
||||||
|
|
||||||
|
var tokenSet = new AntiforgeryTokenSet("form-token", "cookie-token", "form", "header");
|
||||||
|
|
||||||
// Act & Assert
|
// Act & Assert
|
||||||
var exception = Assert.Throws<AntiforgeryValidationException>(
|
var exception = Assert.Throws<AntiforgeryValidationException>(
|
||||||
() => antiforgery.ValidateTokens(
|
() => antiforgery.ValidateTokens(
|
||||||
context.HttpContext,
|
context.HttpContext,
|
||||||
new AntiforgeryTokenSet("form-token", "cookie-token")));
|
tokenSet));
|
||||||
Assert.Equal("my-message", exception.Message);
|
Assert.Equal("my-message", exception.Message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -464,8 +467,10 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
context.TokenStore = null;
|
context.TokenStore = null;
|
||||||
var antiforgery = GetAntiforgery(context);
|
var antiforgery = GetAntiforgery(context);
|
||||||
|
|
||||||
|
var tokenSet = new AntiforgeryTokenSet("form-token", "cookie-token", "form", "header");
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
antiforgery.ValidateTokens(context.HttpContext, new AntiforgeryTokenSet("form-token", "cookie-token"));
|
antiforgery.ValidateTokens(context.HttpContext, tokenSet);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
context.TokenGenerator.Verify();
|
context.TokenGenerator.Verify();
|
||||||
|
|
@ -478,8 +483,7 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
var context = CreateMockContext(new AntiforgeryOptions());
|
var context = CreateMockContext(new AntiforgeryOptions());
|
||||||
var antiforgery = GetAntiforgery(context);
|
var antiforgery = GetAntiforgery(context);
|
||||||
|
|
||||||
var tokenSet = new AntiforgeryTokenSet("hi", cookieToken: null);
|
var tokenSet = new AntiforgeryTokenSet("form-token", null, "form", "header");
|
||||||
|
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
ExceptionAssert.ThrowsArgument(
|
ExceptionAssert.ThrowsArgument(
|
||||||
|
|
@ -661,7 +665,9 @@ namespace Microsoft.AspNetCore.Antiforgery
|
||||||
mockTokenStore.Setup(o => o.GetRequestTokensAsync(context))
|
mockTokenStore.Setup(o => o.GetRequestTokensAsync(context))
|
||||||
.Returns(() => Task.FromResult(new AntiforgeryTokenSet(
|
.Returns(() => Task.FromResult(new AntiforgeryTokenSet(
|
||||||
testTokenSet.FormTokenString,
|
testTokenSet.FormTokenString,
|
||||||
testTokenSet.OldCookieTokenString)));
|
testTokenSet.OldCookieTokenString,
|
||||||
|
"form",
|
||||||
|
"header")));
|
||||||
|
|
||||||
if (saveNewCookie)
|
if (saveNewCookie)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue