Ejemplo n.º 1
0
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);

            var keyByteArray = Encoding.ASCII.GetBytes("Y2F0Y2hlciUyMHdvbmclMjBsb3ZlJTIwLm5ldA==");
            var signingKey   = new SymmetricSecurityKey(keyByteArray);

            var tokenValidationParameters = new TokenValidationParameters
            {
                // The signing key must match!
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = signingKey,

                // Validate the JWT Issuer (iss) claim
                ValidateIssuer = true,
                ValidIssuer    = "http://www.c-sharpcorner.com/members/catcher-wong",

                // Validate the JWT Audience (aud) claim
                ValidateAudience = true,
                ValidAudience    = "Catcher Wong",

                // Validate the token expiry
                ValidateLifetime = true,

                ClockSkew = TimeSpan.Zero
            };

            var configuration = new JwtBearerAuthenticationConfiguration
            {
                TokenValidationParameters = tokenValidationParameters,
                Challenge = "Guest"//if not use this,default to Bearer
            };

            pipelines.EnableJwtBearerAuthentication(configuration);
        }
        public JwtBearerAuthenticationFixture()
        {
            var keyByteArray = Encoding.ASCII.GetBytes("Y2F0Y2hlciUyMHdvbmclMjBsb3ZlJTIwLm5ldA==");
            var signingKey   = new SymmetricSecurityKey(keyByteArray);

            var tokenValidationParameters = new TokenValidationParameters
            {
                // The signing key must match!
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = signingKey,

                // Validate the JWT Issuer (iss) claim
                ValidateIssuer = true,
                ValidIssuer    = "http://www.c-sharpcorner.com/members/catcher-wong",

                // Validate the JWT Audience (aud) claim
                ValidateAudience = true,
                ValidAudience    = "Catcher Wong",

                // Validate the token expiry
                ValidateLifetime = true,

                ClockSkew = TimeSpan.Zero
            };

            this.config = new JwtBearerAuthenticationConfiguration()
            {
                TokenValidationParameters = tokenValidationParameters
            };
            this.hooks = new Pipelines();
            JwtBearerAuthentication.Enable(this.hooks, this.config);
        }
Ejemplo n.º 3
0
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            base.ApplicationStartup(container, pipelines);
            var tokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                IssuerSigningKey         = Jwt.JwtManager.SigningKey(),

                ValidateIssuer = true,
                ValidIssuer    = Jwt.JwtManager.ValidIssuer,

                ValidateAudience = true,
                ValidAudiences   = Jwt.JwtManager.ValidAudiences,

                ValidateLifetime = true,
                ClockSkew        = TimeSpan.Zero
            };

            var configuration = new JwtBearerAuthenticationConfiguration
            {
                TokenValidationParameters = tokenValidationParameters
            };

            pipelines.EnableJwtBearerAuthentication(configuration);
        }
Ejemplo n.º 4
0
        public static IFixture Customizations()
        {
            var fixture = new Fixture();

            // TODO - Set JWT authentication config settings if enabled
            var jwtBearerAuthenticationConfiguration = new JwtBearerAuthenticationConfiguration
            {
                AllowExpiredTokens = true,
                Audience           = "<TODO>",
                Authority          = "<TODO>",
                Enabled            = false,
                OpenApi            = new OpenApiJwtBearerAuthenticationConfiguration
                {
                    AuthorizationUrl = "<TODO>",
                    ClientId         = "<TODO>",
                    TokenUrl         = "<TODO>"
                },
                UseStubbedBackchannelHandler = true
            };

            fixture.Register <IOptions <JwtBearerAuthenticationConfiguration> >(() => jwtBearerAuthenticationConfiguration.AsOption());

            return(fixture);
        }
Ejemplo n.º 5
0
 public static bool IsDisabled(this JwtBearerAuthenticationConfiguration jwtBearerAuthenticationConfiguration) =>
 !jwtBearerAuthenticationConfiguration.IsEnabled();
Ejemplo n.º 6
0
 public static bool HasOpenApiClient(this JwtBearerAuthenticationConfiguration jwtBearerAuthenticationConfiguration) =>
 jwtBearerAuthenticationConfiguration.IsEnabled() &&
 jwtBearerAuthenticationConfiguration.OpenApi != null;
Ejemplo n.º 7
0
 public static bool IsEnabled(this JwtBearerAuthenticationConfiguration jwtBearerAuthenticationConfiguration) =>
 jwtBearerAuthenticationConfiguration != null && jwtBearerAuthenticationConfiguration.Enabled;
Ejemplo n.º 8
0
        private void AddSwagger(
            IServiceCollection services,
            JwtBearerAuthenticationConfiguration jwtBearerAuthenticationConfiguration = null)
        {
            services
            //Add swagger for all endpoints without any filter
            .AddSwaggerGen(c =>
            {
                c.SwaggerDoc("all", new OpenApiInfo
                {
                    Version     = "all",
                    Title       = "Menu API",
                    Description = "APIs used to interact and manage menus for a restaurant",
                    Contact     = new OpenApiContact()
                    {
                        Name  = "Amido",
                        Url   = new Uri(projectUrl),
                        Email = "*****@*****.**"
                    },
                    //TermsOfService = new Uri("http://www.amido.com/")
                });

                //Load comments to show as examples and descriptions in the swagger page
                c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{typeof(Startup).Assembly.GetName().Name}.xml");
                c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{typeof(CreateMenuRequest).Assembly.GetName().Name}.xml");

                //Set default tags, shows on top, non defined tags appears at bottom
                c.DocumentFilter <SwaggerDocumentTagger>(new OpenApiTag[] {
                    new OpenApiTag {
                        Name = "Menu"
                    },
                    new OpenApiTag {
                        Name = "Category"
                    },
                    new OpenApiTag {
                        Name = "Item"
                    }
                }, new string[] { });

                //By Default, all endpoints are grouped by the controller name
                //We want to Group by Api Group first, then by controller name if not provided
                c.TagActionsBy((api) => new[] { api.GroupName ?? api.ActionDescriptor.RouteValues["controller"] });

                c.DocInclusionPredicate((docName, apiDesc) => { return(true); });

                // Use method name as operationId
                c.CustomOperationIds(apiDesc =>
                {
                    return(apiDesc.TryGetMethodInfo(out MethodInfo methodInfo) ? methodInfo.Name : null);
                });

                c.ConfigureForJwtBearerAuthentication(jwtBearerAuthenticationConfiguration);
            })

            //Add swagger for v1 endpoints only
            .AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version     = "v1",
                    Title       = "Menu API",
                    Description = "APIs used to interact and manage menus for a restaurant",
                    Contact     = new OpenApiContact()
                    {
                        Name  = "Amido",
                        Url   = new Uri(projectUrl),
                        Email = "*****@*****.**"
                    },
                    //TermsOfService = new Uri("http://www.amido.com/")
                });

                c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{this.GetType().Assembly.GetName().Name}.xml");

                // Show only operations where route starts with
                c.DocumentFilter <VersionPathFilter>("/v1");
            })

            //Add swagger for v2 endpoints only
            .AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v2", new OpenApiInfo
                {
                    Version     = "v2",
                    Title       = "Menu API",
                    Description = "APIs used to interact and manage menus for a restaurant",
                    Contact     = new OpenApiContact()
                    {
                        Name  = "Amido",
                        Url   = new Uri(projectUrl),
                        Email = "*****@*****.**"
                    },
                    //TermsOfService = new Uri("http://www.amido.com/")
                });

                c.IncludeXmlComments($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{this.GetType().Assembly.GetName().Name}.xml");

                // Show only operations where route starts with
                c.DocumentFilter <VersionPathFilter>("/v2");
            });
        }