squash a bunch of commits
This commit is contained in:
39
Shogi.Sockets/ExampleAnonymousSessionMiddleware.cs
Normal file
39
Shogi.Sockets/ExampleAnonymousSessionMiddleware.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
namespace Shogi.Api
|
||||
{
|
||||
namespace anonymous_session.Middlewares
|
||||
{
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using System.Security.Claims;
|
||||
|
||||
/// <summary>
|
||||
/// TODO: Use this example in the guest session logic instead of custom claims.
|
||||
/// </summary>
|
||||
public class ExampleAnonymousSessionMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
|
||||
public ExampleAnonymousSessionMiddleware(RequestDelegate next)
|
||||
{
|
||||
_next = next;
|
||||
}
|
||||
|
||||
public async System.Threading.Tasks.Task InvokeAsync(HttpContext context)
|
||||
{
|
||||
if (!context.User.Identity.IsAuthenticated)
|
||||
{
|
||||
if (string.IsNullOrEmpty(context.User.FindFirstValue(ClaimTypes.Anonymous)))
|
||||
{
|
||||
var claim = new Claim(ClaimTypes.Anonymous, System.Guid.NewGuid().ToString());
|
||||
context.User.AddIdentity(new ClaimsIdentity(new[] { claim }));
|
||||
|
||||
string scheme = Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationDefaults.AuthenticationScheme;
|
||||
await context.SignInAsync(scheme, context.User, new AuthenticationProperties { IsPersistent = false });
|
||||
}
|
||||
}
|
||||
|
||||
await _next(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user