该问题涉及ASP.NET Core 2.0中的身份验证。
PasswordSignInAsync()
后,result
的状态为成功。因此,我要重定向到Chat
中的操作StaticController
。
[HttpPost("login")]
public async Task<ActionResult> Login([Bind] LoginModel lm) {
if (!ModelState.IsValid)
return BadRequest(ModelState);
var result = await _signInManager.PasswordSignInAsync(lm.Username, lm.Password, false, false);
if (result.Succeeded)
return RedirectToAction("Login", "Static");
else
return BadRequest(new { message = "Invalid username or password" });
}
StaticController.Chat()
[HttpGet("/chat")]
public ActionResult Chat() {
if (User.Identity.IsAuthenticated)
return File("~/chat/index.html", "text/html");
else
return RedirectToAction("Login", "Static");
}
这里出现问题是因为User.Identity.IsAuthenticated始终为false。即使执行了Login()操作后,我也会从浏览器localhost:5000/chat
调用它。仍然是假的。
这些是我发现的相关解决方案,但都不起作用:
此解决方案已过时。我使用ASP.NET Core 2.0并且UseCookieAuthentication()
已经过时了。
User.Identity.IsAuthenticated always false after PasswordSignInAsync gives success
正如它在Login()操作后在此解决方案User.Identity.IsAuthenticated returns false after SignInAsync invoked中所述,我应该再次调用Chat()方法(就像我localhost:5000/chat
所做的那样)并且它应该可以工作。那么问题是它没有。
这是我的Startup.cs:
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.Configure<IdentityOptions>(options => {
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = false;
options.Password.RequireLowercase = false;
// User settings
options.User.RequireUniqueEmail = true;
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseMvc();
app.UseAuthentication();
}
}
请帮忙。
答案 0 :(得分:0)
这是我的startup.cs
public class Startup
{
public IConfiguration Configuration { get; private set; }
public Startup(IHostingEnvironment env)
{
var config = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: true)
.SetBasePath(env.ContentRootPath)
.Build();
Configuration = config;
}
public void ConfigureServices(IServiceCollection services)
{
services.ConfigureDataContext(Configuration);
services.AddIdentity<ApplicationUser, IdentityRole>(p =>
{
p.Password.RequireDigit = false;
p.Password.RequireLowercase = false;
p.Password.RequireUppercase = false;
p.Password.RequireNonAlphanumeric = false;
})
.AddEntityFrameworkStores<MyShuttleContext>()
.AddDefaultTokenProviders();
services.ConfigureDependencies();
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseStaticFiles();
app.UseAuthentication();
app.ConfigureRoutes();
}
}
这是我的config.json
{
"DefaultUsername": "carrier",
"DefaultPassword": "******",
"Data": {
"UseInMemoryStore": "false",
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=MyShuttle;Trusted_Connection=True;MultipleActiveResultSets=true"
}
},
"EntityFramework": {
"MyShuttleContext": {
"ConnectionStringKey": "Data:DefaultConnection:ConnectionString"
}
}
}
我的控制器代码与您的相同。我对这段代码没有任何问题。您可以尝试检查SQL Server对象资源管理器以检查表是否正确填充。
答案 1 :(得分:0)
据我所知,User.Identity.IsAuthenticated
仅在您的控制器(或您的操作)使用[Authorize]
属性进行修饰时才会出现。所以这里:
[HttpGet("/chat")]
[Authorize]
public ActionResult Chat() {
if (User.Identity.IsAuthenticated)
return File("~/chat/index.html", "text/html");
else
return RedirectToAction("Login", "Static");
}