【问题标题】:Asp.Net Core 2.2 Identity AuthenticationAsp.Net Core 2.2 身份认证
【发布时间】:2019-10-18 11:41:41
【问题描述】:

我的网络应用程序出现了一种奇怪的情况,我可以成功登录但未通过身份验证。我检查了属性: User.Identity.IsAuthenticated 属性,其值为 false。我正在使用默认帐户控制器

var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);

我放了一个断点,结果的值为 Success 但 User.Identity.IsAuthenticated 为 false。

下面是我在 Startup 类中的 ConfigureServices 方法的代码:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<SchoolIdentityContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));

        services.AddIdentity<User, IdentityRole>()
            .AddEntityFrameworkStores<SchoolIdentityContext>()
            .AddDefaultTokenProviders();

        services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
            options.LoginPath = "/Account/Signin";
            options.LogoutPath = "/Account/Signout";
            options.AccessDeniedPath = "/Account/AccessDenied";
            options.SlidingExpiration = true;
        });

        services.AddScoped(typeof(IAppLogger<>), typeof(LoggerAdapter<>));
        services.AddScoped<IBookCategoryService, BookCategoryService>();
        services.AddScoped<IBookService, BookService>();

        services.AddHttpClient("chikoroapi", c => 
        {
            c.BaseAddress = new Uri("http://localhost:5100/api");
        });

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

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

            // User settings.
            options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
            options.User.RequireUniqueEmail = true;
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        _services = services;
    }

配置方法如下

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            ListAllRegisteredServices(app);
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            //app.UseHsts();
        }

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

我已经多次与文档进行交叉检查,但我无法弄清楚我遗漏了什么。

【问题讨论】:

    标签: c# authentication asp.net-core asp.net-identity


    【解决方案1】:

    SignIn 为将来的请求保留给定的信息,它不会在当前请求上设置 HttpContext.User。

    您只能将User.Identity.IsAuthenticated 作为后续登录请求。

    参考https://github.com/aspnet/Security/issues/1318

    【讨论】:

    • 这仍然适用于 2.2 吗?它似乎是为 2.0 设计的
    • 在 2.0 或 2.2 中没有什么不同。PasswordSignInAsync 发出一个包含登录信息的 Cookie,直到发出另一个请求并连同它一起发送 Cookie 时才会读取该 Cookie。尝试将 [Authorize] 添加到端点,然后在那里检查 User.Identity.IsAuthenticated 的值
    • 如果我在要重定向到的控制器上添加 Authorize 属性,则会收到 ERR_SPDY_PROTOCOL_ERROR。很抱歉打扰您,您有什么可以推荐的关于 ASP.NET Identiy 的好教程。我来自 MVC5,所以我需要将我的应用程序迁移到 ASP.NET Core。
    • 你看过asp.net core identity doc的认证和授权吗?
    猜你喜欢
    • 1970-01-01
    • 2019-09-06
    • 1970-01-01
    • 2019-12-13
    • 2020-03-21
    • 2019-06-13
    • 2019-08-22
    • 2017-03-30
    • 2018-06-25
    相关资源
    最近更新 更多