26 lines
1.2 KiB
C#
26 lines
1.2 KiB
C#
using Helpdesk.Web.Api;
|
|
using Helpdesk.Web.Auth;
|
|
using Microsoft.AspNetCore.Authentication;
|
|
using Microsoft.AspNetCore.Authentication.Cookies;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace Helpdesk.Web.Tests;
|
|
|
|
internal static class WebTestSession
|
|
{
|
|
public static void SignIn(IServiceProvider services, HttpClient client, string role)
|
|
{
|
|
var scheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
|
var options = services.GetRequiredService<IOptionsMonitor<CookieAuthenticationOptions>>().Get(scheme);
|
|
var now = DateTimeOffset.UtcNow;
|
|
var properties = new AuthenticationProperties { IssuedUtc = now, ExpiresUtc = now.AddHours(1) };
|
|
AuthCookie.StoreTokens(properties, new TokenSet("test-access", now.AddMinutes(15), "test-refresh", now.AddDays(1)));
|
|
var user = new UserDto(1, "[email protected]", "Test", "User", null, role, true);
|
|
var ticket = new AuthenticationTicket(AuthCookie.CreatePrincipal(user), properties, scheme);
|
|
|
|
client.DefaultRequestHeaders.Remove("Cookie");
|
|
client.DefaultRequestHeaders.Add("Cookie", $"{AuthCookie.Name}={options.TicketDataFormat.Protect(ticket)}");
|
|
}
|
|
}
|