Blazor and Identity

非 Y 不嫁゛ 提交于 2020-01-11 13:53:29

问题


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)

So user inherit from IdentityUser which is part of :

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

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.

All kind of help is welcomed !

Thank you


回答1:


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.

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!



来源:https://stackoverflow.com/questions/58661485/blazor-and-identity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!