Fix issue with missing configuration for Facebook or Google
This commit is contained in:
@@ -220,10 +220,13 @@ public class IdentityService(
|
|||||||
}
|
}
|
||||||
|
|
||||||
var user = await GetUserByUserNameAsync(userName);
|
var user = await GetUserByUserNameAsync(userName);
|
||||||
|
|
||||||
|
var jwtSection = configuration.GetRequiredSection("Authentication:Jwt");
|
||||||
|
|
||||||
var token = JwtTokenHelper.GenerateJwtToken(
|
var token = JwtTokenHelper.GenerateJwtToken(
|
||||||
issuer: configuration["Jwt-Issuer"] ?? "",
|
issuer: jwtSection["Issuer"] ?? "",
|
||||||
audience: configuration["Jwt-Audience"] ?? "",
|
audience: jwtSection["Audience"] ?? "",
|
||||||
key: configuration["Jwt-Key"] ?? "",
|
key: jwtSection["Key"] ?? "",
|
||||||
userId: user?.Id ?? "");
|
userId: user?.Id ?? "");
|
||||||
|
|
||||||
return token;
|
return token;
|
||||||
|
|||||||
@@ -44,13 +44,16 @@ public static class DependencyInjection
|
|||||||
configure.Title = "Hutopy API";
|
configure.Title = "Hutopy API";
|
||||||
|
|
||||||
// Add JWT
|
// Add JWT
|
||||||
configure.AddSecurity("JWT", Enumerable.Empty<string>(), new OpenApiSecurityScheme
|
configure.AddSecurity(
|
||||||
{
|
"JWT",
|
||||||
Type = OpenApiSecuritySchemeType.ApiKey,
|
[],
|
||||||
Name = "Authorization",
|
new OpenApiSecurityScheme
|
||||||
In = OpenApiSecurityApiKeyLocation.Header,
|
{
|
||||||
Description = "Type into the textbox: Bearer {your JWT token}."
|
Type = OpenApiSecuritySchemeType.ApiKey,
|
||||||
});
|
Name = "Authorization",
|
||||||
|
In = OpenApiSecurityApiKeyLocation.Header,
|
||||||
|
Description = "Type into the textbox: Bearer {your JWT token}."
|
||||||
|
});
|
||||||
|
|
||||||
configure.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
|
configure.OperationProcessors.Add(new AspNetCoreOperationSecurityScopeProcessor("JWT"));
|
||||||
});
|
});
|
||||||
@@ -58,7 +61,8 @@ public static class DependencyInjection
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddKeyVaultIfConfigured(this IServiceCollection services, ConfigurationManager configuration)
|
public static IServiceCollection AddKeyVaultIfConfigured(this IServiceCollection services,
|
||||||
|
ConfigurationManager configuration)
|
||||||
{
|
{
|
||||||
var keyVaultUri = configuration["KeyVaultUri"];
|
var keyVaultUri = configuration["KeyVaultUri"];
|
||||||
if (!string.IsNullOrWhiteSpace(keyVaultUri))
|
if (!string.IsNullOrWhiteSpace(keyVaultUri))
|
||||||
@@ -71,9 +75,10 @@ public static class DependencyInjection
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddAuthorizationAndAuthentication(this IServiceCollection services, ConfigurationManager configuration)
|
public static IServiceCollection AddAuthorizationAndAuthentication(this IServiceCollection services,
|
||||||
|
ConfigurationManager configuration)
|
||||||
{
|
{
|
||||||
services.AddAuthentication(options =>
|
var authenticationBuilder = services.AddAuthentication(options =>
|
||||||
{
|
{
|
||||||
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
||||||
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
|
||||||
@@ -82,35 +87,50 @@ public static class DependencyInjection
|
|||||||
{
|
{
|
||||||
options.LoginPath = "/api/Users/login";
|
options.LoginPath = "/api/Users/login";
|
||||||
})
|
})
|
||||||
.AddCookie()
|
.AddCookie();
|
||||||
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, jwtBearerOptions =>
|
|
||||||
|
var authJwt = configuration.GetSection("Authentication:Jwt");
|
||||||
|
if (authJwt.Exists())
|
||||||
|
{
|
||||||
|
authenticationBuilder.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, jwtBearerOptions =>
|
||||||
{
|
{
|
||||||
jwtBearerOptions.Authority = "https://hutopy.com";
|
jwtBearerOptions.Authority = "https://hutopy.com";
|
||||||
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
|
jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
|
||||||
{
|
{
|
||||||
ValidateIssuer = true,
|
ValidateIssuer = true,
|
||||||
ValidIssuer = configuration["Jwt-Issuer"],
|
ValidIssuer = authJwt["Issuer"],
|
||||||
ValidateAudience = true,
|
ValidateAudience = true,
|
||||||
ValidAudience = configuration["Jwt-Audience"],
|
ValidAudience = authJwt["Audience"],
|
||||||
ValidateLifetime = true,
|
ValidateLifetime = true,
|
||||||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt-Key"] ??
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(authJwt["Key"] ??
|
||||||
throw new ArgumentNullException("The Jwt Key is missing.")))
|
throw new ArgumentNullException("The Jwt Key is missing.")))
|
||||||
};
|
};
|
||||||
})
|
});
|
||||||
.AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
|
}
|
||||||
|
|
||||||
|
var authGoogle = configuration.GetSection("Authentication:Google");
|
||||||
|
if (authGoogle.Exists())
|
||||||
|
{
|
||||||
|
authenticationBuilder.AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
|
||||||
{
|
{
|
||||||
options.ClientId = configuration["Google-ClientId"] ??
|
options.ClientId = authGoogle["ClientId"] ??
|
||||||
throw new ArgumentNullException("The Google ClientId is missing.");;
|
throw new ArgumentNullException("The Google ClientId is missing.");
|
||||||
options.ClientSecret = configuration["Google-ClientSecret"] ??
|
options.ClientSecret = authGoogle["ClientSecret"] ??
|
||||||
throw new ArgumentNullException("The Google ClientSecret is missing.");;
|
throw new ArgumentNullException("The Google ClientSecret is missing.");
|
||||||
})
|
});
|
||||||
.AddFacebook(FacebookDefaults.AuthenticationScheme, options =>
|
}
|
||||||
|
|
||||||
|
var authFacebook = configuration.GetSection("Authentication:Facebook");
|
||||||
|
if (authFacebook.Exists())
|
||||||
|
{
|
||||||
|
authenticationBuilder.AddFacebook(FacebookDefaults.AuthenticationScheme, options =>
|
||||||
{
|
{
|
||||||
options.ClientId = configuration["Facebook-ClientId"] ??
|
options.ClientId = authFacebook["ClientId"] ??
|
||||||
throw new ArgumentNullException("The Facebook ClientId is missing.");
|
throw new ArgumentNullException("The Facebook ClientId is missing.");
|
||||||
options.ClientSecret = configuration["Facebook-ClientSecret"] ??
|
options.ClientSecret = authFacebook["ClientSecret"] ??
|
||||||
throw new ArgumentNullException("The Facebook ClientSecret is missing.");
|
throw new ArgumentNullException("The Facebook ClientSecret is missing.");
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user