本文介绍了SignInManager< TUser> .PasswordSignInAsync()引发System.Text.Json对象周期异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前,我决定将旧的API项目从.net core 2.0重写为3.1,但是今天我遇到了一个无法解决的错误.重写"AccountManager"后,类_signInManager.PasswordSignInAsync()开始引发与Json对象周期有关的错误.

Few days ago I decided to rewrite old API project from .net core 2.0 to 3.1 but today I faced with an error which I cannot solve. After rewriting "AccountManager" class _signInManager.PasswordSignInAsync() starts to throw error connected with Json object cycle.

例外:

用法示例:

public class AccountManager : IAccountManager{

        public ApplicationUser ActualUser { get; private set; }
        private readonly SignInManager<ApplicationUser> _signInManager;

        public AccountManager(SignInManager<ApplicationUser> signInManager)
        {
            _signInManager = signInManager;
        }

        public async Task<LoginResponseDTO> LoginAsync(LoginRequestDTO input)
        {
            var result = await _signInManager.PasswordSignInAsync(input.Email, input.Password, false, true);
            ...
        }
}

启动:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            ...
            var builder = services.AddIdentityCore<ApplicationUser>(u =>
            {
                ...
            })
            .AddEntityFrameworkStores<RBDbContext>().AddDefaultTokenProviders()
            .AddSignInManager<UserManager<ApplicationUser>>()
            .AddUserManager<UserManager<ApplicationUser>>();
            builder.AddEntityFrameworkStores<RBDbContext>().AddDefaultTokenProviders();
            services.AddScoped<IAccountManager,AccountManager>();
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

我什至尝试对用户和其他类属性[JsonIgnore]进行设置,但没有成功.应用程序用户类:

I even tried to user and other classses attribute [JsonIgnore], but it didn't worked.Application user class:

    public class ApplicationUser : IdentityUser
    {
        public string Avatar { get; set; }
        ...

        [JsonIgnore]
        public virtual ICollection<Order> Orders{ get; set; }
        [JsonIgnore]
        public virtual ICollection<Participation> Participations{ get; set; }
        [JsonIgnore]
        public virtual ICollection<UserProduct> UserProducts{ get; set; }
    }

UserManager来自Microsoft.Extensions.Identity.Core,版本= 3.1.2.0

UserManager comes from Microsoft.Extensions.Identity.Core, Version=3.1.2.0

推荐答案

在ASP.NET Core 3.0+中,它使用 JSON序列化的默认,并且 Newtonsoft.Json 的某些功能可能无法与System.Text.Json配合使用(例如,Newtonsoft.Json默认情况下没有最大深度限制),这可能会导致此问题.

In ASP.NET Core 3.0+, it uses System.Text.Json by default for JSON serialization, and some features of Newtonsoft.Json may not work well with System.Text.Json (for example, Newtonsoft.Json doesn't have a maximum depth limit by default), which might cause this issue.

您可以尝试安装 Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet软件包,使用以下代码片段添加对基于Newtonsoft.Json的格式化程序和功能的支持,并检查它是否对您有用.

You can try to install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package to add support for Newtonsoft.Json based formatters and features with following code snippet, and check if it works for you.

services.AddControllersWithViews().AddNewtonsoftJson(options =>
{
    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
});

此外,您还可以从此处找到Newtonsoft.Json和System.Text.Json之间的区别: https://docs.microsoft.com/zh-cn/dotnet/标准/序列化/系统文本json从newtonsoft迁移到newtonsoftjson和systemtextjson之间的差异表的方法

Besides, you can find differences between Newtonsoft.Json and System.Text.Json from here: https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to#table-of-differences-between-newtonsoftjson-and-systemtextjson

这篇关于SignInManager&lt; TUser&gt; .PasswordSignInAsync()引发System.Text.Json对象周期异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:38