Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

Veysel MUTLU changes #1756

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@ private List<CatalogItem> ChangeUriPlaceholder(List<CatalogItem> items)
var baseUri = _settings.PicBaseUrl;
var azureStorageEnabled = _settings.AzureStorageEnabled;

foreach (var item in items)
{
item.FillProductUrl(baseUri, azureStorageEnabled: azureStorageEnabled);
}
items.ForEach(item => { item.FillProductUrl(baseUri, azureStorageEnabled: azureStorageEnabled); });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of the change?


return items;
}
Expand Down
44 changes: 11 additions & 33 deletions src/Services/Catalog/Catalog.API/Controllers/PicController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,40 +56,18 @@ public async Task<ActionResult> GetImageAsync(int catalogItemId)

private string GetImageMimeTypeFromImageFileExtension(string extension)
{
string mimetype;

switch (extension)
string mimetype = extension switch
{
case ".png":
mimetype = "image/png";
break;
case ".gif":
mimetype = "image/gif";
break;
case ".jpg":
case ".jpeg":
mimetype = "image/jpeg";
break;
case ".bmp":
mimetype = "image/bmp";
break;
case ".tiff":
mimetype = "image/tiff";
break;
case ".wmf":
mimetype = "image/wmf";
break;
case ".jp2":
mimetype = "image/jp2";
break;
case ".svg":
mimetype = "image/svg+xml";
break;
default:
mimetype = "application/octet-stream";
break;
}

".png" => "image/png",
".gif" => "image/gif",
".jpg" or ".jpeg" => "image/jpeg",
".bmp" => "image/bmp",
".tiff" => "image/tiff",
".wmf" => "image/wmf",
".jp2" => "image/jp2",
".svg" => "image/svg+xml",
_ => "application/octet-stream",
};
return mimetype;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ public static void FillProductUrl(this CatalogItem item, string picBaseUrl, bool
{
if (item != null)
{
item.PictureUri = azureStorageEnabled
? picBaseUrl + item.PictureFileName
: picBaseUrl.Replace("[0]", item.Id.ToString());
// https://www.linkedin.com/feed/update/urn:li:activity:6841055191211491328/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this link to the Linkedin post here? It didn't answer the question what is better to use

item.PictureUri = azureStorageEnabled switch
{
true => picBaseUrl + item.PictureFileName,
_ => picBaseUrl.Replace("[0]", item.Id.ToString())
};
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Devspaces
public class DevspacesRedirectUriValidator : IRedirectUriValidator
{
private readonly ILogger _logger;
public DevspacesRedirectUriValidator(ILogger<DevspacesRedirectUriValidator> logger)
{
_logger = logger;
}
public DevspacesRedirectUriValidator(ILogger<DevspacesRedirectUriValidator> logger) => (_logger) = (logger);

public Task<bool> IsPostLogoutRedirectUriValidAsync(string requestedUri, Client client)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using IdentityServer4.Services;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.Services.Identity.API;
using Microsoft.eShopOnContainers.Services.Identity.API.Certificates;
using Microsoft.eShopOnContainers.Services.Identity.API.Data;
using Microsoft.eShopOnContainers.Services.Identity.API.Devspaces;
using Microsoft.eShopOnContainers.Services.Identity.API.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Reflection;
using Microsoft.eShopOnContainers.Services.Identity.API.Services;

namespace Identity.API.Extensions
{
public static class StartupConfigurationExtensions
{
public static void RegisterAppInsights(this IServiceCollection services, IConfiguration configuration)
{
services.AddApplicationInsightsTelemetry(configuration);
services.AddApplicationInsightsKubernetesEnricher();
}

// Add framework services.
public static void ConfigureDatabase(this IServiceCollection services, IConfiguration configuration)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
}));

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
}

// Adds IdentityServer
public static void ConfigureIdentityServer(this IServiceCollection services, IConfiguration configuration)
{
var connectionString = configuration["ConnectionString"];
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

services.AddIdentityServer(x =>
{
x.IssuerUri = "null";
x.Authentication.CookieLifetime = TimeSpan.FromHours(2);
})
.AddDevspacesIfNeeded(configuration.GetValue("EnableDevspaces", false))
.AddSigningCredential(Certificate.Get())
.AddAspNetIdentity<ApplicationUser>()
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(migrationsAssembly);
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(migrationsAssembly);
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
})
.Services.AddTransient<IProfileService, ProfileService>();
}

public static void ConfigureDependecyInjections(this IServiceCollection services)
{
services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
services.AddTransient<IRedirectService, RedirectService>();
}
}
}
4 changes: 0 additions & 4 deletions src/Services/Identity/Identity.API/Identity.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,4 @@
</None>
</ItemGroup>

<ItemGroup>
<Folder Include="Extensions\" />
</ItemGroup>

</Project>
2 changes: 2 additions & 0 deletions src/Services/Identity/Identity.API/Services/ProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public ProfileService(UserManager<ApplicationUser> userManager)
_userManager = userManager;
}

//New Method ArgumentNullException.ThrowIfNull (obj) Added to .NET 6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure this source code is not a guide for C# new features, it is more about architecture.
Why do we need to include links to posts? Why not official documentation then?

//https://davidshergilashvili.space/2021/09/08/new-method-argumentnullexception-throwifnull-obj-added-to-net-6/
async public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var subject = context.Subject ?? throw new ArgumentNullException(nameof(context.Subject));
Expand Down
72 changes: 6 additions & 66 deletions src/Services/Identity/Identity.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,34 @@
using Autofac;
using Autofac.Extensions.DependencyInjection;
using HealthChecks.UI.Client;
using IdentityServer4.Services;
using Identity.API.Extensions;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.eShopOnContainers.Services.Identity.API.Certificates;
using Microsoft.eShopOnContainers.Services.Identity.API.Data;
using Microsoft.eShopOnContainers.Services.Identity.API.Devspaces;
using Microsoft.eShopOnContainers.Services.Identity.API.Models;
using Microsoft.eShopOnContainers.Services.Identity.API.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
using System;
using System.Reflection;

namespace Microsoft.eShopOnContainers.Services.Identity.API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public Startup(IConfiguration configuration) => (Configuration) = (configuration);

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
RegisterAppInsights(services);
services.RegisterAppInsights(Configuration);

// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["ConnectionString"],
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(typeof(Startup).GetTypeInfo().Assembly.GetName().Name);
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
}));

services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.ConfigureDatabase(Configuration);

services.Configure<AppSettings>(Configuration);

Expand All @@ -69,42 +47,10 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
name: "IdentityDB-check",
tags: new string[] { "IdentityDB" });

services.AddTransient<ILoginService<ApplicationUser>, EFLoginService>();
services.AddTransient<IRedirectService, RedirectService>();

var connectionString = Configuration["ConnectionString"];
var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;
services.ConfigureDependecyInjections();

// Adds IdentityServer
services.AddIdentityServer(x =>
{
x.IssuerUri = "null";
x.Authentication.CookieLifetime = TimeSpan.FromHours(2);
})
.AddDevspacesIfNeeded(Configuration.GetValue("EnableDevspaces", false))
.AddSigningCredential(Certificate.Get())
.AddAspNetIdentity<ApplicationUser>()
.AddConfigurationStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(migrationsAssembly);
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
})
.AddOperationalStore(options =>
{
options.ConfigureDbContext = builder => builder.UseSqlServer(connectionString,
sqlServerOptionsAction: sqlOptions =>
{
sqlOptions.MigrationsAssembly(migrationsAssembly);
//Configuring Connection Resiliency: https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-resiliency
sqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
});
})
.Services.AddTransient<IProfileService, ProfileService>();
services.ConfigureIdentityServer(Configuration);

//services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddControllers();
Expand Down Expand Up @@ -174,11 +120,5 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
});
});
}

private void RegisterAppInsights(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddApplicationInsightsKubernetesEnricher();
}
}
}
19 changes: 19 additions & 0 deletions src/Services/Ordering/Ordering.API/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,25 @@ public static IServiceCollection AddCustomSwagger(this IServiceCollection servic
}
});

/*
* resource: https://twitter.com/CoderBora/status/1436392448356495362
* Swagger => Authorization: Bearer {token} : DO YOU WANT TO WRITE "Bearer" to the Value each time?

AddSecurityDefinition=>{ in Swagger

Type = SecuritySchemeType Choose "Http" instead of "ApiKey". Avoid typing "Bearer" instead of token
*
*/
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = @"JWT Authorization header using the Bearer scheme. Example: \'Authorization: Bearer {token}\'",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.Http,
Scheme = "Bearer"
});


options.OperationFilter<AuthorizeCheckOperationFilter>();
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.eShopOnContainers.Services.Ordering.Domain.Seedwork;

namespace Ordering.Infrastructure.EntityConfigurations
{
public class BaseConfiguration<TEntity> : IEntityTypeConfiguration<TEntity> where TEntity : Entity
{
public virtual void Configure(EntityTypeBuilder<TEntity> builder)
{
builder.Ignore(b => b.DomainEvents);
builder.HasKey(e => e.Id);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@

namespace Ordering.Infrastructure.EntityConfigurations
{
class BuyerEntityTypeConfiguration
: IEntityTypeConfiguration<Buyer>
class BuyerEntityTypeConfiguration : BaseConfiguration<Buyer>
{
public void Configure(EntityTypeBuilder<Buyer> buyerConfiguration)
public override void Configure(EntityTypeBuilder<Buyer> buyerConfiguration)
{
buyerConfiguration.ToTable("buyers", OrderingContext.DEFAULT_SCHEMA);

buyerConfiguration.HasKey(b => b.Id);

buyerConfiguration.Ignore(b => b.DomainEvents);

buyerConfiguration.Property(b => b.Id)
.UseHiLo("buyerseq", OrderingContext.DEFAULT_SCHEMA);

Expand All @@ -36,6 +31,8 @@ public void Configure(EntityTypeBuilder<Buyer> buyerConfiguration)
var navigation = buyerConfiguration.Metadata.FindNavigation(nameof(Buyer.PaymentMethods));

navigation.SetPropertyAccessMode(PropertyAccessMode.Field);

base.Configure(buyerConfiguration);
}
}
}
Loading