Exemplo n.º 1
0
 public AuthorizeController(
     UserManager <User> userManager,
     SignInManager <User> signInManager,
     UserStore userStore,
     IJsonWebTokenGenerationService jsonWebTokenGenerationService,
     IMessageBroker messageBroker,
     IOptions <VkontakteOptions> options
     )
 {
     _vkontakteOptions = options.Value;
     _userManager      = userManager;
     _signInManager    = signInManager;
     _userStore        = userStore;
     _jsonWebTokenGenerationService = jsonWebTokenGenerationService;
     _messageBroker = messageBroker;
 }
Exemplo n.º 2
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded    = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            services
            .AddMvcCore()
            .AddCors(opts =>
            {
                opts.AddPolicy("CorsPolicy", builder =>
                               builder
                               .WithOrigins(_configuration["FrontendUrl"])
                               .AllowAnyHeader()
                               .AllowCredentials()
                               .AllowAnyMethod()
                               );
            })
            .AddJsonOptions(options =>
            {
                new JsonSerializerOptionsProvider().Apply(options.JsonSerializerOptions);
            });

            services.AddHttpContextAccessor();
            services.Configure <EncryptionOptions>(_configuration.GetSection("EncryptionOptions"));
            services.AddSingleton <IEncryptionService, EncryptionService>();

            services
            .AddControllers(x => { x.UseGeneralRoutePrefix("api"); });

            var vkOptions = new VkontakteOptions();

            _configuration.GetSection(nameof(VkontakteOptions)).Bind(vkOptions);
            services.Configure <VkontakteOptions>(_configuration.GetSection(nameof(VkontakteOptions)));

            services.AddMetricsAuthentication(_configuration, builder =>
            {
                builder.AddCookie()
                .AddOAuth <VkOauthOptions, VkHandler>("Vkontakte", "Vkontakte", options =>
                {
                    options.SignInScheme = IdentityConstants.ExternalScheme;
                    options.ClientId     = vkOptions.AppId;
                    options.ClientSecret = vkOptions.AppSecret;
                    options.CallbackPath = new PathString("/sign-in");
                    options.Scope.Add(vkOptions.AppScope);
                    options.Fields     = vkOptions.Fields;
                    options.ApiVersion = vkOptions.ApiVersion;
                    options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
                    options.ClaimActions.MapJsonKey(ClaimTypes.Name, "first_name");
                    options.ClaimActions.MapJsonKey(ClaimTypes.Surname, "last_name");
                    options.ClaimActions.MapJsonKey("photo", "photo_50");
                    options.SaveTokens = true;
                });
            });

            services.AddHttpClient();

            services.AddMetricsIdentity(_configuration.GetConnectionString(nameof(IdentityContext)));

            services.ConfigureApplicationCookie(options =>
            {
                options.Events.OnRedirectToLogin = context =>
                {
                    context.Response.StatusCode = StatusCodes.Status401Unauthorized;
                    return(Task.CompletedTask);
                };
            });

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Identity API", Version = "v1"
                });
            });

            services.AddMessageBroker(_configuration, (collection, configuration) => new RedisBrokerConfigurationBuilder(_configuration, services));
            services.AddGrpc();
        }