我在asp.net Core 2中遇到身份验证问题
当我使用网页浏览器(以visual studio调试模式开始)时
User.Identity.IsAuthenticated
工作没有问题,但是当我尝试从其他域或邮递员(Chrome扩展程序)
使用此API时 User.Identity.IsAuthenticated
总是假的!
这是我的登录代码:
var result = await _signInManager.PasswordSignInAsync("user","pass",true,false);
这是我的startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add database
services.AddDbContext<CoreDbContext>(options =>
{
options.UseSqlServer(ConnectionString);
options.EnableSensitiveDataLogging(true);
}
);
// add sign in manager
services.AddIdentity<User, Role>(options =>
{
// Password settings
options.Password.RequireDigit = false;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
// Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
options.Lockout.MaxFailedAccessAttempts = 10;
// User settings
options.User.RequireUniqueEmail = false;
options.SignIn.RequireConfirmedEmail = false;
options.SignIn.RequireConfirmedEmail = false;
})
.AddEntityFrameworkStores<CoreDbContext>()
.AddDefaultTokenProviders();
services.AddCors();
services.AddMvc().AddJsonOptions(options =>
{
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
services.Configure<GzipCompressionProviderOptions>(options => options.Level = System.IO.Compression.CompressionLevel.Optimal);
services.AddResponseCompression();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
DbOption.Option = new DbContextOptionsBuilder<CoreDbContext>();
DbOption.Option.UseSqlServer(ConnectionString);
DbOption.Option.EnableSensitiveDataLogging(true);
app.UseResponseCompression();
app.UseCors(builder =>
builder.WithOrigins("http://localhost:4200", "http://192.168.30.100:91", "OtherDomain.com")
.AllowAnyHeader().AllowAnyMethod().AllowCredentials());
app.UseAuthentication();
app.UseMvc();
}
当我使用网络浏览器时,所有工作都没有任何问题。 我认为这是针对cookie的问题。