Skip to content

Instantly share code, notes, and snippets.

@Skidaddle99
Last active October 20, 2023 09:17
Show Gist options
  • Save Skidaddle99/6b30952a43ac51aa86d49b1c6f2fe9f9 to your computer and use it in GitHub Desktop.
Save Skidaddle99/6b30952a43ac51aa86d49b1c6f2fe9f9 to your computer and use it in GitHub Desktop.
Ucommerce Alpha 8 Template Breaking Changes
using alpha8mvc; //Your project name here
using Ucommerce.Extensions.InProcess.Abstractions.Extensions;
using Ucommerce.Extensions.Payment.Abstractions.Extensions;
using Ucommerce.Extensions.Search.Abstractions.Extensions;
using Ucommerce.Search.Elastic.Configuration;
using Ucommerce.Web.BackOffice.DependencyInjection;
using Ucommerce.Web.Core.DependencyInjection;
using Ucommerce.Web.Infrastructure.DependencyInjection;
using Ucommerce.Web.WebSite.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Set up services
builder.Services.AddUcommerce(builder.Configuration)
//.AddBackOffice(opt => { opt.Security.Requirements.Add(new AssertionRequirement(_ => true)); })
.AddBackOffice() //Default requirement is no longer needed
.AddWebSite()
.AddInProcess<ContextParser>()
.UcommerceBuilder
.AddSearch()
.AddElasticSearch()
.WithSingleNodeSetup()
.AddPayments()
.Build();
builder.Services.AddControllersWithViews();
var app = builder.Build();
// configure and run
app.UseTestUser();
app.UseUcommerce()
.UseEndpoints(u => { u.UseUcommerceEndpoints(); }) //This needs to be used before UseBackOfficeUi
.UseBackOfficeUi()
.UsePayments();
app.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}");
app.Run();
//Replace your old test user middleware with this one
using System.Security.Claims;
namespace alpha8mvc; //Your namespace here
/// <summary>
/// Middleware for setting the test user
/// </summary>
public class SetTestUserMiddleware
{
private readonly RequestDelegate _next;
/// <summary>
/// Constructor that takes the next middleware
/// </summary>
/// <param name="next">The next middleware</param>
/// <exception cref="ArgumentNullException">If next is null</exception>
public SetTestUserMiddleware(RequestDelegate next)
{
_next = next ?? throw new ArgumentNullException(nameof(next));
}
/// <summary>
/// Invoke the middleware
/// </summary>
/// <param name="context"></param>
public async Task InvokeAsync(HttpContext context)
{
try
{
var userName = "admin@admin.com";
var claims = new List<Claim>
{
new(ClaimTypes.NameIdentifier, userName),
new(ClaimTypes.Name, userName) //Added this
};
var identity = new ClaimsIdentity(claims, authenticationType: "Test"); //Added authentication type which is now needed
context.User = new ClaimsPrincipal(identity);
await _next(context);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
}
/// <summary>
/// Setting up a pipeline for the test user middleware.
/// </summary>
public static class TestUserExtensions
{
/// <summary>
/// Configures a pipeline for test user middleware.
/// </summary>
public static void UseTestUser(this IApplicationBuilder applicationBuilder)
{
applicationBuilder.UseMiddleware<SetTestUserMiddleware>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment