Asp.net core authenticationStateProvider; _localStorage=localStorage; } 公共异步任务寄存器(ApplicationUserModel registerModel) { var result=await_http

Asp.net core authenticationStateProvider; _localStorage=localStorage; } 公共异步任务寄存器(ApplicationUserModel registerModel) { var result=await_http,asp.net-core,blazor,webassembly,blazor-client-side,Asp.net Core,Blazor,Webassembly,Blazor Client Side,authenticationStateProvider; _localStorage=localStorage; } 公共异步任务寄存器(ApplicationUserModel registerModel) { var result=await_httpClient.PostJsonAsync(BaseUrl+“create”,registerModel); 返回结果; } } 您能发布响应日志吗?请400不是401。这可能与您的DTO有关,而不是与授权有关。发布关于ApplicationUs

authenticationStateProvider; _localStorage=localStorage; } 公共异步任务寄存器(ApplicationUserModel registerModel) { var result=await_httpClient.PostJsonAsync(BaseUrl+“create”,registerModel); 返回结果; } }
您能发布响应日志吗?请400不是401。这可能与您的DTO有关,而不是与授权有关。发布关于ApplicationUserModel的所有详细信息。除了上面两条注释外,您还需要检查这一行:
returnbadRequest(…)以确保这不是预期的结果。因此,我决定将其推送到github,您可以了解整个细节,谢谢
namespace EssentialShopCoreBlazor.Server
{
    public class Startup
    {
        public IConfiguration Configuration { get; }
        public Startup(IConfiguration _configuration)
        {
            Configuration = _configuration;
        }

        readonly string AllowSpecificOrigins = "allowSpecificOrigins";
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<DbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

            services.AddIdentity<IdentityUsersModel, IdentityRole>()
                .AddEntityFrameworkStores<DbContext>()
                .AddDefaultTokenProviders();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>{
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuer = true,
                    ValidateAudience = true,
                    ValidateLifetime = true,
                    ValidateIssuerSigningKey = true,
                    ValidIssuer = Configuration["JwtIssuer"],
                    ValidAudience = Configuration["JwtAudience"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtSecurityKey"]))
                };
            });

            services.AddCors(options =>
            {
                options.AddPolicy(AllowSpecificOrigins,
                builder =>
                {
                    builder.WithOrigins("https://localhost:44365", "https://localhost:44398")
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
            });

            services.AddScoped<AccountAuthController>();


            services.AddMvc().AddNewtonsoftJson();
            services.AddResponseCompression(opts =>
            {
                opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
                    new[] { "application/octet-stream" });
            });
        }


       // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseResponseCompression();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBlazorDebugging();
            }

            app.UseCors(AllowSpecificOrigins);
            app.UseStaticFiles();
            app.UseClientSideBlazorFiles<Client.Startup>();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
                endpoints.MapFallbackToClientSideBlazor<Client.Startup>("index.html");
            });
        }
    }
}

namespace EssentialShopCoreBlazor.Server.Controllers
{
    [Route("essential/users/")]
    public class AccountAuthController : ControllerBase
    {

        private static UserModel LoggedOutUser = new UserModel { IsAuthenticated = false };

        private readonly UserManager<IdentityUsersModel> userManager;
        private readonly IConfiguration configuration;
        private readonly SignInManager<IdentityUsersModel> signInManager;

        public AccountAuthController(UserManager<IdentityUsersModel> _userManager, SignInManager<IdentityUsersModel> _signInManager, IConfiguration _configuration)
        {
            userManager = _userManager;
            signInManager = _signInManager;
            configuration = _configuration;
        }

        [HttpPost]
        [Route("create")]
        public async Task<ActionResult<ApplicationUserModel>> CreateUser([FromBody] ApplicationUserModel model)
        {
            var NewUser = new IdentityUsersModel
            {
                UserName = model.UserName,
                BusinessName = model.BusinessName,
                Email = model.Email,
                PhoneNumber = model.PhoneNumber
            };

            var result = await userManager.CreateAsync(NewUser, model.Password);

            if (!result.Succeeded)
            {
                var errors = result.Errors.Select(x => x.Description);
                return BadRequest(new RegistrationResult { Successful = false, Errors = errors });
            }
            return Ok(new RegistrationResult { Successful = true });
        }
}}
  public class AuthService : IAuthService
    {
        private readonly HttpClient _httpClient;
        private readonly AuthenticationStateProvider _authenticationStateProvider;
        private readonly ILocalStorageService _localStorage;

        string BaseUrl = "https://localhost:44398/essential/users/";

        public AuthService(HttpClient httpClient,
                           AuthenticationStateProvider authenticationStateProvider,
                           ILocalStorageService localStorage)
        {
            _httpClient = httpClient;
            _authenticationStateProvider = authenticationStateProvider;
            _localStorage = localStorage;
        }

        public async Task<RegistrationResult> Register(ApplicationUserModel registerModel)
        {
            var result = await _httpClient.PostJsonAsync<RegistrationResult>(BaseUrl + "create", registerModel);

            return result;
        }
}