40 lines
1.2 KiB
C#
40 lines
1.2 KiB
C#
namespace Gameboard.ShogiUI.Sockets
|
|
{
|
|
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 AnonymousSessionMiddleware
|
|
{
|
|
private readonly RequestDelegate _next;
|
|
|
|
public AnonymousSessionMiddleware(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);
|
|
}
|
|
}
|
|
}
|
|
}
|