Exemplo n.º 1
0
        public IActionResult Post([FromBody] LoginModel loginModel)
        {
            if (loginModel.Username != "fabio" && loginModel.Password != "erivelto33")
            {
                return(Unauthorized());
            }

            var token = new JwtTokenBuilder()
                        .AddSecurityKey(JwtSecurtyKey.Create("a-password-very-big-to-be-good"))
                        .AddSubject("Fabio Erivelto")
                        .AddIssuer("http://*****:*****@gmail.com")
                        .AddClaim("employeer", "31")
                        .AddExpiry(1)
                        .Build();

            return(Ok(token));
        }
Exemplo 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.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(options => {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer           = true,
                    RequireExpirationTime    = true,
                    ValidateAudience         = true,
                    ValidateLifetime         = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer      = "http://localhost:12796",
                    ValidAudience    = "http://localhost:12796",
                    IssuerSigningKey = JwtSecurtyKey.Create("a-password-very-big-to-be-good")
                };

                options.Events = new JwtBearerEvents
                {
                    OnAuthenticationFailed = context =>
                    {
                        Console.WriteLine("OnAuthenticationFailed: " + context.Exception.Message);
                        return(Task.CompletedTask);
                    },
                    OnTokenValidated = context =>
                    {
                        Console.WriteLine("OnTokenValidated: " + context.SecurityToken);
                        return(Task.CompletedTask);
                    }
                };
            });

            services.AddMvc();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info {
                    Title = "Cadastro", Version = "v1"
                });
            });
        }