forked from dotnet/blazor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable same-origin credentials by default. Add E2E test to show they …
…can be sent to different-origin domains too.
- Loading branch information
1 parent
fd5637a
commit ad1431b
Showing
10 changed files
with
193 additions
and
10 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
28 changes: 28 additions & 0 deletions
28
src/Microsoft.AspNetCore.Blazor.Browser/Http/FetchCredentialsOption.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,28 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Blazor.Browser.Http | ||
{ | ||
/// <summary> | ||
/// Specifies a value for the 'credentials' option on outbound HTTP requests. | ||
/// </summary> | ||
public enum FetchCredentialsOption | ||
{ | ||
/// <summary> | ||
/// Advises the browser never to send credentials (such as cookies or HTTP auth headers). | ||
/// </summary> | ||
Omit, | ||
|
||
/// <summary> | ||
/// Advises the browser to send credentials (such as cookies or HTTP auth headers) | ||
/// only if the target URL is on the same origin as the calling application. | ||
/// </summary> | ||
SameOrigin, | ||
|
||
/// <summary> | ||
/// Advises the browser to send credentials (such as cookies or HTTP auth headers) | ||
/// even for cross-origin requests. | ||
/// </summary> | ||
Include, | ||
} | ||
} |
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
38 changes: 38 additions & 0 deletions
38
test/testapps/BasicTestApp/HttpClientTest/CookieCounterComponent.cshtml
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,38 @@ | ||
@inject System.Net.Http.HttpClient Http | ||
|
||
<h1>Cookie counter</h1> | ||
<p>The server increments the count by one on each request.</p> | ||
<p>TestServer base URL: <input @bind(testServerBaseUrl) /></p> | ||
<button id="delete" @onclick(DeleteCookie)>Delete cookie</button> | ||
<button id="increment" @onclick(GetAndIncrementCounter)>Get and increment current value</button> | ||
|
||
@if (!requestInProgress) | ||
{ | ||
<p id="response-text">@responseText</p> | ||
} | ||
|
||
@functions | ||
{ | ||
bool requestInProgress = false; | ||
string testServerBaseUrl; | ||
string responseText; | ||
|
||
async void DeleteCookie() | ||
{ | ||
await DoRequest("api/cookie/reset"); | ||
StateHasChanged(); | ||
} | ||
|
||
async void GetAndIncrementCounter() | ||
{ | ||
await DoRequest("api/cookie/increment"); | ||
StateHasChanged(); | ||
} | ||
|
||
async Task DoRequest(string url) | ||
{ | ||
requestInProgress = true; | ||
responseText = await Http.GetStringAsync(testServerBaseUrl + url); | ||
requestInProgress = false; | ||
} | ||
} |
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,32 @@ | ||
using Microsoft.AspNetCore.Cors; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace TestServer.Controllers | ||
{ | ||
[Route("api/[controller]/[action]")] | ||
[EnableCors("AllowAll")] // Only because the test client apps runs on a different origin | ||
public class CookieController : Controller | ||
{ | ||
const string cookieKey = "test-counter-cookie"; | ||
|
||
public string Reset() | ||
{ | ||
Response.Cookies.Delete(cookieKey); | ||
return "Reset completed"; | ||
} | ||
|
||
public string Increment() | ||
{ | ||
var counter = 0; | ||
if (Request.Cookies.TryGetValue(cookieKey, out var incomingValue)) | ||
{ | ||
counter = int.Parse(incomingValue); | ||
} | ||
|
||
counter++; | ||
Response.Cookies.Append(cookieKey, counter.ToString()); | ||
|
||
return $"Counter value is {counter}"; | ||
} | ||
} | ||
} |
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