Exemplo n.º 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            string connectionString = Configuration.GetConnectionString("Mongo");

            services.AddMongoContext(connectionString);

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            .AddCookie(options =>
            {
                options.LoginPath = new PathString("/Account/Login");
                options.Events.OnRedirectToLogin = context =>
                {
                    if (context.Request.Path.Value.StartsWith("/api"))
                    {
                        context.Response.Clear();
                        context.Response.StatusCode = 401;
                        return(Task.FromResult(0));
                    }
                    context.Response.Redirect(context.RedirectUri);
                    return(Task.FromResult(0));
                };
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.Events = new JwtBearerEvents
                {
                    OnMessageReceived = context =>
                    {
                        if (context.Request.Path.Value.StartsWith("/Account/Register") || context.Request.Path.Value.StartsWith("/Account/Certificate"))
                        {
                            context.Token = context.Request.Query["token"];
                        }
                        return(Task.CompletedTask);
                    }
                };
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    ValidIssuer              = RegisterTokenOptions.ISSUER,
                    ValidateAudience         = true,
                    ValidAudience            = RegisterTokenOptions.AUDIENCE,
                    ValidateLifetime         = true,
                    IssuerSigningKey         = RegisterTokenOptions.GetSymmetricSecurityKey(),
                    ValidateIssuerSigningKey = true,
                };
            });

            services.AddAuthorization();
            services.Configure <LoggerSetting>(_loggerConfig);
            services.AddMapper();
            services.RegisterServices(Configuration);
            services.AddMvc();
        }
Exemplo n.º 2
0
        public string GenerateRegistrationToken(RegistrationParameters parameters)
        {
            var claims = new List <Claim>
            {
                new Claim(RegistrationParameters.CompanyClaim, parameters.CompanyId.ToString()),
                new Claim(RegistrationParameters.RoleClaim, parameters.Role),
                new Claim(RegistrationParameters.PositionClaim, parameters.Position),
            };
            var now = DateTime.UtcNow;
            var jwt = new JwtSecurityToken(
                issuer: RegisterTokenOptions.ISSUER,
                audience: RegisterTokenOptions.AUDIENCE,
                notBefore: now,
                claims: claims,
                expires: now.Add(TimeSpan.FromMinutes(RegisterTokenOptions.LIFETIME)),
                signingCredentials: new SigningCredentials(RegisterTokenOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256));
            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            return(encodedJwt);
        }