-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
deb1b48
commit d054a24
Showing
13 changed files
with
171 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
...zor.Cookies.SampleApp/BitzArt.Blazor.Cookies.SampleApp.Client/Components/Pages/Home.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
@page "/" | ||
@rendermode InteractiveAuto | ||
|
||
<PageTitle>Blazor.Cookies | Home</PageTitle> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
src/BitzArt.Blazor.Cookies.Server/Extensions/AddBlazorCookiesExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/BitzArt.Blazor.Cookies.Server/Extensions/SameSiteModeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
namespace BitzArt.Blazor.Cookies.Server; | ||
|
||
internal static class SameSiteModeExtensions | ||
{ | ||
/// <summary> | ||
/// Converts BitzArt.Blazor.Cookies.SameSiteMode values to Microsoft.AspNetCore.Http.SameSiteMode values | ||
/// </summary> | ||
public static Microsoft.AspNetCore.Http.SameSiteMode ToHttp(this BitzArt.Blazor.Cookies.SameSiteMode? sameSiteMode) | ||
=> sameSiteMode switch | ||
{ | ||
SameSiteMode.None => Microsoft.AspNetCore.Http.SameSiteMode.None, | ||
SameSiteMode.Lax => Microsoft.AspNetCore.Http.SameSiteMode.Lax, | ||
SameSiteMode.Strict => Microsoft.AspNetCore.Http.SameSiteMode.Strict, | ||
null => Microsoft.AspNetCore.Http.SameSiteMode.Unspecified, | ||
|
||
_ => throw new ArgumentOutOfRangeException(nameof(sameSiteMode), sameSiteMode, null) | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
namespace BitzArt.Blazor.Cookies; | ||
|
||
/// <summary> | ||
/// <see href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value">SameSiteMode</see> | ||
/// controls whether or not a cookie is sent with cross-site requests, providing some protection against cross-site request forgery attacks | ||
/// (<see href="https://developer.mozilla.org/en-US/docs/Glossary/CSRF">CSRF</see>). | ||
/// </summary> | ||
public enum SameSiteMode | ||
{ | ||
/// <summary> | ||
/// Indicates the client should disable same-site restrictions. | ||
/// </summary> | ||
None = 0, | ||
|
||
/// <summary> | ||
/// Indicates the client should send the cookie with "same-site" requests, and with | ||
/// "cross-site" top-level navigations. | ||
/// </summary> | ||
Lax = 1, | ||
|
||
/// <summary> | ||
/// Indicates the client should only send the cookie with "same-site" requests. | ||
/// </summary> | ||
Strict = 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.Text; | ||
|
||
namespace BitzArt.Blazor.Cookies; | ||
|
||
internal static class JsCommand | ||
{ | ||
public static string SetCookie(Cookie cookie) | ||
{ | ||
var builder = new StringBuilder(); | ||
|
||
builder.Append("document.cookie = \""); | ||
|
||
builder.Append($"{cookie.Key}={cookie.Value}; "); | ||
builder.Append($"expires={cookie.Expiration:R}; "); | ||
builder.Append("path=/"); | ||
|
||
if (cookie.SameSiteMode.HasValue) | ||
{ | ||
builder.Append("; "); | ||
builder.Append($"SameSite={cookie.SameSiteMode.Value.ToString()}"); | ||
} | ||
|
||
builder.Append('\"'); | ||
|
||
return builder.ToString(); | ||
} | ||
} |
Oops, something went wrong.