当我尝试登录用户时出现错误:
public async Task<bool> SignIn(string email, string password)
{
var user = await this._userManager.FindByEmailAsync(email);
if (user == null)
return false;
if (await this._userManager.CheckPasswordAsync(user, password))
{
await this._signInManager.SignInAsync(user, false); //error
return true;
}
return false;
}
我的Startup.cs如下所示:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
builder.AddUserSecrets();
}
builder.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfigurationRoot Configuration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<RoleManager<IdentityRole>>();
services.AddTransient<UserManager<ApplicationUser>>();
services.AddTransient<SignInManager<ApplicationUser>>();
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<DbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<DbContext>()
.AddDefaultTokenProviders();
services.AddMvc(config =>
{
#if !DEBUG
config.Filters.Add(new RequireHttpsAttribute());
#endif
}).AddJsonOptions(opts =>
{
opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}); ;
services.AddSingleton<IAccountService, AccountService>();
services.AddSingleton<IAgencyService, AgencyService>();
services.AddSingleton<IFeatureService, FeatureService>();
....
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
try
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
serviceScope.ServiceProvider.GetService<DbContext>()
.Database.Migrate();
}
}
catch { }
}
app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
app.UseStaticFiles();
app.UseIdentity();
app.UseMvc(routes =>
{
... routes
});
SeedGenerator.Initialize(app.ApplicationServices);
}
public static void Main(string[] args) => WebApplication.Run<Startup>(args);