我将AWS-CognitoIdentity Provider纳入我的ASP.Net Core Web Api项目中,并且在遵循官方文档后,我仍然获得HttpContext.User NULL。是否存在某人成功使用过的逐步指南,才能使AWS-CognitoIdentity提供程序正常工作。
我已经在Startup.cs中设置了CognitoIdentity,后来又在其他控制器中尝试访问用户。
public class Startup
{
private static readonly ILog logger = LogManager.GetLogger(typeof(Startup));
private string poolId;
private string appClientId;
private static string providerName;
private static AmazonCognitoIdentityProviderClient provider;
private static CognitoUserPool pool;
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
appClientId = Configuration.GetValue<string>("AWS:UserPoolClientId");
providerName = Configuration.GetValue<string>("AWS:ProviderName");
poolId = Configuration.GetValue<string>("AWS:UserPoolId");
AWSConfigs.RegionEndpoint = RegionEndpoint.EUWest2;
provider = new AmazonCognitoIdentityProviderClient();
pool = new CognitoUserPool(poolId, appClientId, provider, "");
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<IdentityOptions>(options =>
{
options.Lockout.MaxFailedAccessAttempts = 10;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
});
services.AddCognitoIdentity();
services.AddAuthentication("Bearer").AddJwtBearer(options =>
{
options.Audience = Configuration.GetValue<string>("AWS:UserPoolClientId");
options.Authority = Configuration.GetValue<string>("AWS:ProviderName");
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = Configuration.GetValue<string>("AWS:ProviderName"),
ValidateIssuerSigningKey = true,
ValidateIssuer = true,
ValidateLifetime = true,
ValidAudience = Configuration.GetValue<string>("AWS:UserPoolClientId"),
ValidateAudience = true,
IssuerSigningKeyResolver = (s, securityToken, identifier, parameters) =>
{
var json = new WebClient().DownloadString(Configuration.GetValue<string>("AWS:MetadataAddress"));
var keys = JsonConvert.DeserializeObject<JsonWebKeySet>(json).Keys;
return (IEnumerable<SecurityKey>)keys;
},
};
options.Events = new JwtBearerEvents
{
OnMessageReceived = context =>
{
Console.WriteLine("Message Received-------------------------------------------------------------\n");
return Task.CompletedTask;
},
OnTokenValidated = context =>
{
Console.WriteLine("TokenValidated Received-------------------------------------------------------\n");
return Task.CompletedTask;
}
};
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
UserManager<CognitoUser> _userManager,
SignInManager<CognitoUser> _signInManager)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseAuthentication();
app.UsePermissions();
app.UseMvc();
app.UseSwagger();
}
}
控制器
namespace DataControllers
{
//[Authorize]
[Route("api/[controller]/[action]")]
public class ContentController : Controller
{
private readonly CognitoUserManager<CognitoUser> _userManager;
public ContentController(UserManager<CognitoUser> userManager)
{
_userManager = userManager as CognitoUserManager<CognitoUser>;
}
[HttpGet]
public async Task<IActionResult> Menu()
{
var email = User.Claims.FirstOrDefault(e => e.Type == "email"); ;
}
}
}