diff --git a/Amphasis.Azure.WebPortal/Program.cs b/Amphasis.Azure.WebPortal/Program.cs index acd4183..1b1acbc 100644 --- a/Amphasis.Azure.WebPortal/Program.cs +++ b/Amphasis.Azure.WebPortal/Program.cs @@ -1,17 +1,132 @@ -using Microsoft.AspNetCore; -using Microsoft.AspNetCore.Hosting; +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Security.Claims; +using System.Threading.Tasks; +using Amphasis.Azure.WebPortal.Models; +using Amphasis.Azure.WebPortal.SimaLand.Models; +using Amphasis.Azure.WebPortal.SimaLand.Services; +using Amphasis.Azure.WebPortal.Yandex.Models; +using Amphasis.SimaLand; +using AspNet.Security.OAuth.MailRu; +using AspNet.Security.OAuth.Vkontakte; +using Microsoft.AspNetCore.Authentication.Cookies; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using Newtonsoft.Json; -namespace Amphasis.Azure.WebPortal +var applicationBuilder = WebApplication.CreateBuilder(args); +var configuration = applicationBuilder.Configuration; +var services = applicationBuilder.Services; + +services.AddMemoryCache(); +services.AddResponseCaching(); +services.AddScoped(); +services.AddScoped(); +services.AddHttpClient(); +services.AddHttpClient(); +services.Configure(configuration.GetSection("Simaland")); +services.Configure(configuration.GetSection("Yandex")); + +services.Configure(options => { - public static class Program + // This lambda determines whether user consent for non-essential cookies is needed for a given request. + options.CheckConsentNeeded = context => true; + options.MinimumSameSitePolicy = SameSiteMode.None; +}); + +services + .AddAuthentication(options => options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme) + .AddCookie(options => + { + options.LoginPath = "/SignIn"; + options.LogoutPath = "/SignOut"; + }) + .AddMailRu(options => { - public static void Main(string[] args) + configuration.GetSection("MailRu").Bind(options); + + options.Events.OnCreatingTicket += context => { - WebHost - .CreateDefaultBuilder(args) - .UseStartup() - .Build() - .Run(); - } - } + var identity = context.Identity; + var originalImageClaim = identity?.FindFirst(MailRuAuthenticationConstants.Claims.ImageUrl); + if (originalImageClaim == null) return Task.CompletedTask; + identity.RemoveClaim(originalImageClaim); + var newImageClaim = new Claim(CustomClaims.UserImageUrl, originalImageClaim.Value, originalImageClaim.ValueType); + identity.AddClaim(newImageClaim); + return Task.CompletedTask; + }; + }) + .AddVkontakte(options => + { + configuration.GetSection("VK").Bind(options); + + options.Events.OnCreatingTicket += context => + { + var identity = context.Identity; + var originalImageClaim = identity?.FindFirst(VkontakteAuthenticationConstants.Claims.PhotoUrl); + if (originalImageClaim == null) return Task.CompletedTask; + identity.RemoveClaim(originalImageClaim); + var newImageClaim = new Claim(CustomClaims.UserImageUrl, originalImageClaim.Value, originalImageClaim.ValueType); + identity.AddClaim(newImageClaim); + return Task.CompletedTask; + }; + }) + .AddYandex(options => + { + configuration.GetSection("Yandex").Bind(options); + + options.Events.OnCreatingTicket += async context => + { + if (context.Identity == null) return; + var uri = new Uri("https://login.yandex.ru/info"); + var authorization = new AuthenticationHeaderValue("OAuth", context.AccessToken); + using var httpClient = new HttpClient(); + httpClient.DefaultRequestHeaders.Authorization = authorization; + using var httpResponseMessage = await httpClient.GetAsync(uri); + httpResponseMessage.EnsureSuccessStatusCode(); + var contentString = await httpResponseMessage.Content.ReadAsStringAsync(); + var userInfo = JsonConvert.DeserializeObject(contentString); + var imageUrl = $"https://avatars.yandex.net/get-yapic/{userInfo.DefaultAvatarId}/islands-200"; + var imageClaim = new Claim(CustomClaims.UserImageUrl, imageUrl, ClaimValueTypes.String); + context.Identity.AddClaim(imageClaim); + }; + }); + +var razorPagesBuilder = services.AddRazorPages(); +services.AddControllers(); + +if (applicationBuilder.Environment.IsDevelopment()) +{ + razorPagesBuilder.AddRazorRuntimeCompilation(); +} + +var application = applicationBuilder.Build(); + +if (application.Environment.IsDevelopment()) +{ + application.UseDeveloperExceptionPage(); +} +else +{ + application.UseExceptionHandler("/Error"); + application.UseHsts(); } + +application.UseHttpsRedirection(); +application.UseResponseCaching(); +application.UseStaticFiles(); +application.UseCookiePolicy(); + +application.UseRouting(); + +application.UseAuthentication(); +application.UseAuthorization(); + +application.MapRazorPages(); +application.MapControllers(); + +application.Run(); diff --git a/Amphasis.Azure.WebPortal/Startup.cs b/Amphasis.Azure.WebPortal/Startup.cs deleted file mode 100644 index a8f98bb..0000000 --- a/Amphasis.Azure.WebPortal/Startup.cs +++ /dev/null @@ -1,151 +0,0 @@ -using System; -using System.Net.Http; -using System.Net.Http.Headers; -using System.Security.Claims; -using System.Threading.Tasks; -using Amphasis.Azure.WebPortal.Models; -using Amphasis.Azure.WebPortal.SimaLand.Models; -using Amphasis.Azure.WebPortal.SimaLand.Services; -using Amphasis.Azure.WebPortal.Yandex.Models; -using Amphasis.SimaLand; -using AspNet.Security.OAuth.MailRu; -using AspNet.Security.OAuth.Vkontakte; -using Microsoft.AspNetCore.Authentication.Cookies; -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Newtonsoft.Json; - -namespace Amphasis.Azure.WebPortal -{ - public class Startup - { - private readonly IConfiguration _configuration; - private readonly IWebHostEnvironment _environment; - - public Startup(IConfiguration configuration, IWebHostEnvironment environment) - { - _configuration = configuration; - _environment = environment; - } - - // This method gets called by the runtime. Use this method to add services to the container. - public void ConfigureServices(IServiceCollection services) - { - services.AddMemoryCache(); - services.AddResponseCaching(); - services.AddScoped(); - services.AddScoped(); - services.AddHttpClient(); - services.AddHttpClient(); - services.Configure(_configuration.GetSection("Simaland")); - services.Configure(_configuration.GetSection("Yandex")); - - services.Configure(options => - { - // This lambda determines whether user consent for non-essential cookies is needed for a given request. - options.CheckConsentNeeded = context => true; - options.MinimumSameSitePolicy = SameSiteMode.None; - }); - - services - .AddAuthentication(options => options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme) - .AddCookie(options => - { - options.LoginPath = "/SignIn"; - options.LogoutPath = "/SignOut"; - }) - .AddMailRu(options => - { - _configuration.GetSection("MailRu").Bind(options); - - options.Events.OnCreatingTicket += context => - { - var identity = context.Identity; - var originalImageClaim = identity?.FindFirst(MailRuAuthenticationConstants.Claims.ImageUrl); - if (originalImageClaim == null) return Task.CompletedTask; - identity.RemoveClaim(originalImageClaim); - var newImageClaim = new Claim(CustomClaims.UserImageUrl, originalImageClaim.Value, originalImageClaim.ValueType); - identity.AddClaim(newImageClaim); - return Task.CompletedTask; - }; - }) - .AddVkontakte(options => - { - _configuration.GetSection("VK").Bind(options); - - options.Events.OnCreatingTicket += context => - { - var identity = context.Identity; - var originalImageClaim = identity?.FindFirst(VkontakteAuthenticationConstants.Claims.PhotoUrl); - if (originalImageClaim == null) return Task.CompletedTask; - identity.RemoveClaim(originalImageClaim); - var newImageClaim = new Claim(CustomClaims.UserImageUrl, originalImageClaim.Value, originalImageClaim.ValueType); - identity.AddClaim(newImageClaim); - return Task.CompletedTask; - }; - }) - .AddYandex(options => - { - _configuration.GetSection("Yandex").Bind(options); - - options.Events.OnCreatingTicket += async context => - { - if (context.Identity == null) return; - var uri = new Uri("https://login.yandex.ru/info"); - var authorization = new AuthenticationHeaderValue("OAuth", context.AccessToken); - using var httpClient = new HttpClient(); - httpClient.DefaultRequestHeaders.Authorization = authorization; - using var httpResponseMessage = await httpClient.GetAsync(uri); - httpResponseMessage.EnsureSuccessStatusCode(); - var contentString = await httpResponseMessage.Content.ReadAsStringAsync(); - var userInfo = JsonConvert.DeserializeObject(contentString); - var imageUrl = $"https://avatars.yandex.net/get-yapic/{userInfo.DefaultAvatarId}/islands-200"; - var imageClaim = new Claim(CustomClaims.UserImageUrl, imageUrl, ClaimValueTypes.String); - context.Identity.AddClaim(imageClaim); - }; - }); - - var razorPagesBuilder = services.AddRazorPages(); - services.AddControllers(); - - if (_environment.IsDevelopment()) - { - razorPagesBuilder.AddRazorRuntimeCompilation(); - } - } - - // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. - public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment environment) - { - if (environment.IsDevelopment()) - { - applicationBuilder.UseDeveloperExceptionPage(); - } - else - { - applicationBuilder.UseExceptionHandler("/Error"); - applicationBuilder.UseHsts(); - } - - applicationBuilder.UseHttpsRedirection(); - applicationBuilder.UseResponseCaching(); - applicationBuilder.UseStaticFiles(); - applicationBuilder.UseCookiePolicy(); - - applicationBuilder.UseRouting(); - - applicationBuilder.UseAuthentication(); - applicationBuilder.UseAuthorization(); - - applicationBuilder.UseEndpoints(builder => - { - builder.MapRazorPages(); - builder.MapControllers(); - }); - } - } -}