西装外套和身份 [英] Blazor and Identity

查看:95
本文介绍了西装外套和身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.net core 3.0中有一个项目,包含3个部分:api,共享和出色的应用程序.

I have a project in .net core 3.0 with 3 parts : api, shared and a blazor app.

我在共享项目中添加了一些模型,并在用户模型中添加了一个模型:

I added few models in shared project and a user model :

    [Table("user")]
    public class User : IdentityUser<Guid>
    {
        [Required(ErrorMessage = "Firstname is required")]
        [StringLength(32, ErrorMessage = "Firstname must be 32 charaters maximum")]
        [Column("firstname")]
        public string Firstname { get; set; }

        [Required(ErrorMessage = "Lastname is required")]
        [StringLength(32, ErrorMessage = "Lastname must be 32 charaters maximum")]
        [Column("lastname")]
        public string Lastname { get; set; }

        [Required(ErrorMessage = "Thumbnail link is required")]
        [StringLength(512, ErrorMessage = "Thumbnail link must be 512 charaters maximum")]
        [Column("thumbnail_link")]
        public string ThumbnailLink { get; set; }

        [Column("birthday")]
        public DateTime Birthday { get; set; }

        [Required(ErrorMessage = "CreationDate is required")]
        [Column("creation_date")]
        public DateTime CreationDate { get; set; }

        // [Required(ErrorMessage = "User role is required")]
        // [Column("user_role")]
        // public UserRole UserRole { get; set; }

        [Required(ErrorMessage = "Is a banned user is required")]
        [Column("is_banned")]
        public IsBanned IsBanned { get; set; }

        public ICollection<UserRole> UserRoles { get; set; }
        public ICollection<UserProgression> UserProgressions { get; set; }
        public ICollection<Playlist> Playlists { get; set; }
        // public ICollection<Message> MessagesReceived { get; set; }
        // public ICollection<Message> MessagesSended { get; set; }
        public ICollection<Rating> Ratings { get; set; }
        public ICollection<Course> Courses { get; set; }
        public ICollection<SubscriptionHistory> SubscriptionHistories { get; set; }

    }

但是当我运行烈火机时,我得到了一个错误

But when I run the blazor side I got an error

Cannot find declaration of exported type 'System.Threading.Semaphore' from the assembly 'System.Threading, Version=4.0.11.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
  Fatal error in IL Linker

  Unhandled Exception: Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'C:\Users\sebastien\.nuget\packages\system.buffers\4.5.0\lib\netstandard2.0\System.Bufers.dll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' ---> Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'C:\Users\sebastien\.nuget\packages\system.buffers\4.5.0\lib\netstandard2.0\System.Bufers.dll, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
     at Mono.Linker.DirectoryAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters)
     at Mono.Linker.AssemblyResolver.Resolve(AssemblyNameReference
name, ReaderParameters parameters)
     at Mono.Linker.LinkContext.Resolve(IMetadataScope scope)
     --- End of inner exception stack trace ---
     at Mono.Linker.LinkContext.Resolve(IMetadataScope scope)
     at Mono.Linker.Steps.ResolveFromAssemblyStep.Process()
     at Mono.Linker.Steps.BaseStep.Process(LinkContext context)
     at Mono.Linker.Pipeline.ProcessStep(LinkContext context, IStep step)
     at Mono.Linker.Pipeline.Process(LinkContext context)
     at Mono.Linker.Driver.Run(ILogger customLogger)
     at Mono.Linker.Driver.Execute(String[] args, ILogger customLogger)
     at Mono.Linker.Driver.Main(String[] args)

因此,用户从IdentityUser继承:

So user inherit from IdentityUser which is part of :

PackageReference Include ="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version ="2.2.0"

PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.2.0"

我发现blazor不支持带有实体框架的软件包.

I found out that blazor doesn't support package with entity framework.

那么我该如何实现身份?如何在服务器端和前端之间共享模型?我是一个初学者,很难找到有关该主题的一些信息.

So how can I implement Identity ? How can I share my Models between my server side and my front end ? I'm a beginner and it's very difficult to find some informations on the subject.

欢迎各种帮助!

谢谢

推荐答案

不幸的是,您无法与blazor应用程序共享IdentityUser模型. Blazor客户端在.Net标准2.0上运行,您的身份资产在Asp.net核心上运行.您可以在asp.net核心端使用.net标准代码,但不能相反.

Unfortunately you can't share an IdentityUser model with the blazor application. Blazor client-side runs on .Net standard 2.0, your identity assets run on Asp.net core. You can use .net standard code on the asp.net core side, but not the other way around.

要解决此问题,请将您的IdentityUser模型移动到服务器,并在共享项目中创建一个带有您要向下传递到客户端的属性的传递模型",然后从实际的AspNetUsers表行中复制每个属性.无论如何,您想完全控制哪些用户可以看到AspNetUsers表.希望这可以帮助!

To get around this, move your IdentityUser model to the server and create a "transfer model" in your shared project with the properties you want to transfer down to the client, then copy each property from the actual AspNetUsers table row. You want to have full control over what user's are able to see about the AspNetUsers table anyway. Hope this helps!

这篇关于西装外套和身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆