gpt4 book ai didi

c# - ASP.NET Core 2.0 Identity : SignInManager. IsSignedIn(User) 登录后返回 false

转载 作者:太空狗 更新时间:2023-10-30 01:00:12 34 4
gpt4 key购买 nike

我遇到一个问题,在创建用户并登录 SignInManager.IsSignedIn(User) 方法后返回 false

我正在使用 SPA Angular CLI 模板的新 2.0.0 预览版,我已经根据我在文档中阅读的内容添加了所有身份配置,并使用其他一些 Visual Studio 模板作为指南.

下面是我的 Startup 类。

public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment hostingEnvironment)
{
Configuration = configuration;
HostingEnvironment = hostingEnvironment;

//configration
MyAppConfig.ConnectionString = Configuration.GetConnectionString("DefaultConnection");
MyAppConfig.Secure = Configuration.GetValue<bool>("App:Secure", true);
}

public IConfiguration Configuration { get; }
private IHostingEnvironment HostingEnvironment { get; set; }
public IContainer ApplicationContainer { get; private set; }

private const string EmailConfirmationTokenProviderName = "ConfirmEmail";

// This method gets called by the runtime. Use this method to add services to the container.
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddMvc();

// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});


IDataProtectionBuilder dataProtectionBuilder = services.AddDataProtection()
.SetApplicationName("MyApp");

//data protection
if (HostingEnvironment.IsDevelopment())
{
//for development, do default (intentionally left blank for now)
}
else
{
//for deployments, protect certificate and persist to azure storage
//this will allow swapping of web app slots
dataProtectionBuilder
.ProtectKeysWithCertificate("")
.PersistKeysToAzureBlobStorage(new Uri(""));
}


//ssl
if (!HostingEnvironment.IsDevelopment())
{
services.Configure<MvcOptions>(options =>
{
options.Filters.Add(new RequireHttpsAttribute());
});
}

var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();

//anti-forgery, add to all controllers
services.AddMvc(options => {
options.Filters.Add(new ValidateAntiForgeryTokenAttribute());
options.Filters.Add(new AuthorizeFilter(policy));
});

services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN");

services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(MyAppConfig.ConnectionString));

//identity
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>()
.AddDefaultTokenProviders()
.AddTokenProvider<ConfirmEmailDataProtectorTokenProvider<ApplicationUser>>(EmailConfirmationTokenProviderName); ;

services.Configure<IdentityOptions>(options =>
{
// Password settings
options.Password.RequireDigit = true;
options.Password.RequiredLength = 8;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = false;
options.Password.RequiredUniqueChars = 4;

// Lockout settings
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;

// User settings
options.User.RequireUniqueEmail = true;

// SignIn settings
options.SignIn.RequireConfirmedEmail = true;
options.SignIn.RequireConfirmedPhoneNumber = false;

options.Tokens.EmailConfirmationTokenProvider = EmailConfirmationTokenProviderName;
});

services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.Cookie.Expiration = TimeSpan.FromHours(1);
options.SlidingExpiration = true;
});

services.Configure<ConfirmEmailDataProtectionTokenProviderOptions>(options =>
{
options.TokenLifespan = TimeSpan.FromDays(180);
});


//autofac
var builder = new ContainerBuilder();

builder.Populate(services);
builder.RegisterType<AccountService>().As<IAccountService>();
builder.RegisterType<EmailService>().As<IEmailService>();

this.ApplicationContainer = builder.Build();

// Create the IServiceProvider based on the container.
return new AutofacServiceProvider(this.ApplicationContainer);

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IAntiforgery antiforgery)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseStaticFiles();
app.UseSpaStaticFiles();

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});

//send anti-forgery cookie with initial SPA page
//must be before UseSpa() call
app.Use(next => context =>
{
if (
string.Equals(context.Request.Path.Value, "/", StringComparison.OrdinalIgnoreCase) ||
string.Equals(context.Request.Path.Value, "/index.html", StringComparison.OrdinalIgnoreCase))
{
// We can send the request token as a JavaScript-readable cookie, and Angular will use it by default.
var tokens = antiforgery.GetAndStoreTokens(context);
context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken,
new CookieOptions() { HttpOnly = false });
}

return next(context);
});

app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501

spa.Options.SourcePath = "ClientApp";

if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
//spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
}
});

//auth & identity
app.UseAuthentication();
}

}

以下是我用来测试用户是否登录的步骤:

  1. 创建用户
  2. 验证电子邮件(然后让请求完成)
  3. 在返回成功的新 HTTP 请求中调用 SignInManager.PasswordSignInAsync()
  4. 让请求返回
  5. 验证 .AspNetCore.Identity.Application cookie 是否存在
  6. 启动另一个调用 SignInManager.IsSignedIn(User) 的 HTTP 请求并返回 false

我不确定问题可能是什么,因为我已经阅读了 https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity?tabs=visual-studio%2Caspnetcore2x 上的文档。

最佳答案

您的中间件按照您在应用中声明的顺序执行。这是一个很棒的resource为了帮助您更好地理解,

enter image description here

当您最后调用身份验证时,这将是管道中完成的最后一件事:

app.UseAuthentication();

要解决此问题,只需将此行放在:

app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});

关于c# - ASP.NET Core 2.0 Identity : SignInManager. IsSignedIn(User) 登录后返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48465397/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com