Esempio n. 1
0
 public AuthController(ElBuenSaborContext context, IAuthService authService, GoogleAuthSettings GoogleAuthSettings, CommonPassSettings CommonPassSettings)
 {
     _context            = context;
     _authService        = authService;
     _GoogleAuthSettings = GoogleAuthSettings;
     _CommonPassSettings = CommonPassSettings;
 }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <ElBuenSaborContext>(
                options => options.UseSqlServer(ConnectionString));

            services.AddControllers().AddNewtonsoftJson(o =>
            {
                o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

            services.AddCors();

            //-----------jwt
            var jwtSettings = new JwtSettings();

            Configuration.Bind(key: nameof(jwtSettings), jwtSettings);

            services.AddSingleton(jwtSettings);

            services.AddAuthentication(configureOptions: x =>
            {
                x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
                x.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(x =>
            {
                x.RequireHttpsMetadata = false;
                x.SaveToken            = true;
                //this will help us validate request as they come to our controllers
                x.TokenValidationParameters = new TokenValidationParameters {
                    //this wil validate the last bit of our jwt is using the secret
                    //and make sure is authentic
                    ValidateIssuerSigningKey = true,
                    //we need a bytearray, and the secret is a string, so we need to use enconding
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secreto)),
                    //Basic jwt authentication
                    ValidateIssuer        = false,
                    ValidateAudience      = false,
                    RequireExpirationTime = false,
                    ValidateLifetime      = true
                };
            }
                            );

            /* Google y su OAuthorization */

            // Es buena practica crear un objeto que tome los valores de appsetting.json
            // y luego acceder a esos valores por medio del objeto, parece que por seguridad
            var GoogleAuthSettings = new GoogleAuthSettings();

            Configuration.Bind(key: nameof(GoogleAuthSettings), GoogleAuthSettings);

            services.AddSingleton(GoogleAuthSettings);

            //Password comun para los usuarios logeados con google
            var CommonPassSettings = new CommonPassSettings();

            Configuration.Bind(key: nameof(CommonPassSettings), CommonPassSettings);

            services.AddSingleton(CommonPassSettings);

            services.AddAuthentication().AddGoogle(googleOptions =>
            {
                IConfigurationSection googleAuthNSection =
                    Configuration.GetSection("Authentication:Google"); //mmm ver, me parece que no apunta a nada


                googleOptions.ClientId     = GoogleAuthSettings.ClientId; // ver appsettings.json
                googleOptions.ClientSecret = GoogleAuthSettings.ClientSecret;
            });

            /*
             * Permite que mediante el contructor de una clase, se pueda inyectar la dependencia
             * IAuthService
             * The AddScoped method registers the service with a scoped lifetime,
             * the lifetime of a single request.
             */
            services.AddScoped <IAuthService, AuthService>();

            //-----------jwt


            /*I have increased the size for Singal R and that fixed the issue for now but this is not a proper solution.
             * The proper solution is to implement your own hub between client and server and process in chunks and stick it together.
             * refer to : https://docs.microsoft.com/en-us/aspnet/core/signalr/streaming?view=aspnetcore-3.1
             */

            services.AddSignalR(e => {
                e.MaximumReceiveMessageSize = 102400000;
                e.EnableDetailedErrors      = true;
            });

            //Din't work to avoid object cycles
            //services.AddControllers().AddJsonOptions(options =>
            //    options.JsonSerializerOptions.MaxDepth = 2
            //);
        }