我很生气。谢谢你应该考虑把这个问题变成一个问题,然后自己回答它作为一个正确的答案。我已经这么做了,你确实帮了我。谢谢。当你没有使用JWT或Identity的经验时,互联网上有很多过时的教程,很难找到解决方案。我想问一下builder=new Identit

我很生气。谢谢你应该考虑把这个问题变成一个问题,然后自己回答它作为一个正确的答案。我已经这么做了,你确实帮了我。谢谢。当你没有使用JWT或Identity的经验时,互联网上有很多过时的教程,很难找到解决方案。我想问一下builder=new Identit,jwt,asp.net-identity-2,asp.net-core-webapi,Jwt,Asp.net Identity 2,Asp.net Core Webapi,我很生气。谢谢你应该考虑把这个问题变成一个问题,然后自己回答它作为一个正确的答案。我已经这么做了,你确实帮了我。谢谢。当你没有使用JWT或Identity的经验时,互联网上有很多过时的教程,很难找到解决方案。我想问一下builder=new IdentityBuilder(builder.UserType,typeof(IdentityRole),builder.Services)does?@t123yh这是AddIdentityCore()方法没有采用所用角色类型的构造函数这一事实的解决方法的


我很生气。谢谢你应该考虑把这个问题变成一个问题,然后自己回答它作为一个正确的答案。我已经这么做了,你确实帮了我。谢谢。当你没有使用JWT或Identity的经验时,互联网上有很多过时的教程,很难找到解决方案。我想问一下
builder=new IdentityBuilder(builder.UserType,typeof(IdentityRole),builder.Services)
does?@t123yh这是AddIdentityCore()方法没有采用所用角色类型的构造函数这一事实的解决方法的一部分(在我的例子中,我只是使用内置的
IdentityRole
类,而从中派生来向角色添加自定义属性是很常见的,在这种情况下,您需要将派生类传递给它)。基本上,这是一种解决方法,可以启用将RoleValidator、RoleManager和SignInManager添加/注册到DI系统的后续行,以便所有中间件都可以使用它们。回答得很好,感谢您还提到了checkpassworkasync调用。不确定为什么这些信息会被隐藏起来,并且很难找到exampnet core中的授权文件。@GPW它似乎工作正常,无需重新实例化IdentityBuilder变量,尽管我遇到的所有帖子都使用了重新实例化方法。var builder=services.AddIdentityCore(o=>…).AddRoles().AddEntityFrameworkStores()+1.我可能会使用这个,但我现在做的很好。这对于让生活更轻松肯定是有用的,希望任何新来这个问题的人现在都有更整洁的代码:)
services.AddIdentity<IdentityUser, IdentityRole>()
    .AddEntityFrameworkStores<MyContext>();
 services.AddAuthentication(
            JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                 >>breakpoint>>>   options.TokenValidationParameters =
                        new TokenValidationParameters
                        {
                            ValidateIssuer = true,
                            ValidateAudience = true,
                            ValidateLifetime = true,
                            ValidateIssuerSigningKey = true,

                            ValidIssuer = "Blah.Blah.Bearer",
                            ValidAudience = "Blah.Blah.Bearer",
                            IssuerSigningKey =
                            JwtSecurityKey.Create("verylongsecretkey")

                        };
                });
services.AddAuthentication(
            JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
...
services.AddAuthentication(options =>
        {
            options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
        })
            .AddJwtBearer(options =>
...
  IdentityBuilder builder = services.AddIdentityCore<IdentityUser>(opt =>
        {
            opt.Password.RequireDigit = true;
            opt.Password.RequiredLength = 8;
            opt.Password.RequireNonAlphanumeric = false;
            opt.Password.RequireUppercase = true;
            opt.Password.RequireLowercase = true;
        }
        );
        builder = new IdentityBuilder(builder.UserType, typeof(IdentityRole), builder.Services);
        builder
            .AddEntityFrameworkStores<MyContext>();
        //.AddDefaultTokenProviders();

        builder.AddRoleValidator<RoleValidator<IdentityRole>>();
        builder.AddRoleManager<RoleManager<IdentityRole>>();
        builder.AddSignInManager<SignInManager<IdentityUser>>();
public async Task<IdentityUser> GetUserForLogin(string userName, string password)
    {   
        //find user first...
        var user = await _userManager.FindByNameAsync(userName);

        if (user == null)
        {
            return null;
        }

        //validate password...
        var signInResult = await _signInManager.CheckPasswordSignInAsync(user, password, false);

        //if password was ok, return this user.
        if (signInResult.Succeeded)
        {
            return user;
        }

        return null;
    }
/// <summary>
/// Contains extension methods to <see cref="IServiceCollection"/> for configuring identity services.
/// </summary>
public static class IdentityServiceExtensions
{
    /// <summary>
    /// Adds the default identity system configuration for the specified User and Role types. (Without Authentication Scheme)
    /// </summary>
    /// <typeparam name="TUser">The type representing a User in the system.</typeparam>
    /// <typeparam name="TRole">The type representing a Role in the system.</typeparam>
    /// <param name="services">The services available in the application.</param>
    /// <returns>An <see cref="IdentityBuilder"/> for creating and configuring the identity system.</returns>
    public static IdentityBuilder AddIdentityWithoutAuthenticator<TUser, TRole>(this IServiceCollection services)
        where TUser : class
        where TRole : class
        => services.AddIdentityWithoutAuthenticator<TUser, TRole>(setupAction: null);

    /// <summary>
    /// Adds and configures the identity system for the specified User and Role types. (Without Authentication Scheme)
    /// </summary>
    /// <typeparam name="TUser">The type representing a User in the system.</typeparam>
    /// <typeparam name="TRole">The type representing a Role in the system.</typeparam>
    /// <param name="services">The services available in the application.</param>
    /// <param name="setupAction">An action to configure the <see cref="IdentityOptions"/>.</param>
    /// <returns>An <see cref="IdentityBuilder"/> for creating and configuring the identity system.</returns>
    public static IdentityBuilder AddIdentityWithoutAuthenticator<TUser, TRole>(this IServiceCollection services, Action<IdentityOptions> setupAction)
        where TUser : class
        where TRole : class
    {
        // Hosting doesn't add IHttpContextAccessor by default
        services.AddHttpContextAccessor();
        // Identity services
        services.TryAddScoped<IUserValidator<TUser>, UserValidator<TUser>>();
        services.TryAddScoped<IPasswordValidator<TUser>, PasswordValidator<TUser>>();
        services.TryAddScoped<IPasswordHasher<TUser>, PasswordHasher<TUser>>();
        services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
        services.TryAddScoped<IRoleValidator<TRole>, RoleValidator<TRole>>();
        // No interface for the error describer so we can add errors without rev'ing the interface
        services.TryAddScoped<IdentityErrorDescriber>();
        services.TryAddScoped<ISecurityStampValidator, SecurityStampValidator<TUser>>();
        services.TryAddScoped<ITwoFactorSecurityStampValidator, TwoFactorSecurityStampValidator<TUser>>();
        services.TryAddScoped<IUserClaimsPrincipalFactory<TUser>, UserClaimsPrincipalFactory<TUser, TRole>>();
        services.TryAddScoped<UserManager<TUser>>();
        services.TryAddScoped<SignInManager<TUser>>();
        services.TryAddScoped<RoleManager<TRole>>();

        if (setupAction != null)
        {
            services.Configure(setupAction);
        }

        return new IdentityBuilder(typeof(TUser), typeof(TRole), services);
    }
}
.AddIdentityWithoutAuthenticator<User, IdentityRole>()