核心3.0 ApplicationUser始终为空

编程入门 行业动态 更新时间:2024-10-19 03:34:48
本文介绍了核心3.0 ApplicationUser始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经扩展了 Identityuser

public class ApplicationUser : IdentityUser { [MaxLength(150)] public string FirstName { get ; set ; } [MaxLength(150)] public string LastName { get ; set ; } public int AlternateUserId { get ; set ; } [MaxLength(150)] public string CompanyName { get ; set ; } [MaxLength(38)] [Required] public string ClientId { get ; set ; } [Required] public int ShortClient { set ; get ; } public bool Locked { set ; get ; } }

在 Startup.cs 中,我有:

services.AddIdentity<ApplicationUser, IdentityRole>().AddDefaultUI().AddEntityFrameworkStores<ApplicationDbContext>(); services.AddSingleton<ApplicationUser>();

但是在

public static class IdentityExtentionMethods { public static string FirstName(this IIdentity identity) { var claim = ((ClaimsIdentity)identity).FindFirst(ClaimTypes.GivenName); // Test for null to avoid issues during local testing return (claim != null) ? claim.Value : string.Empty; } }

声明始终为null,并且在我尝试注入 ApplicationUser 的任何位置,该变量可用,但未填充用户信息.

Claim is always null and anywhere I try to inject ApplicationUser the variable is available but it is not populated with the user information.

@inject ApplicationUser applicationUser @inject SignInManager<ApplicationUser> signInManager;

相反,它在一些 Guid 字段中具有一些伪值,而其他大多数东西都是空的.

Instead it has some dummy values in a few of the Guid fields and most everything else is null.

推荐答案

正如Chris Pratt指出的那样,您无法通过Core 3.x中的注入获得ApplicationUser,我不确定较早的版本.更糟糕的是,它不在我能看到的任何地方的文档中.

As Chris Pratt points out you can't get ApplicationUser via injection in Core 3.x I am not sure about earlier versions. To bad its not in the documentation anywhere that I could see.

但是你可以得到

SignInManager<ApplicationUser> _signInManager, UserManager <ApplicationUser> _userManager ApplicationDbContext _dbContext

正如克里斯还指出的那样,您可以得到

And as Chris also points out you can get

ClaimsPrincipal

IPrincipal

我有IPrincipal,如下面的代码所示,使用SignInManager&您需要UserManager来获取ApplicationUser

I have IPrincipal and as the code shows below, that with SignInManager & UserManager is all you need to get ApplicationUser

public static class IdentityExtentionMethods { public static bool IsSysAdmin(this IPrincipal _principal, SignInManager<ApplicationUser> _signInManager, UserManager <ApplicationUser> _userManager) { var x = isSysAdmin(_principal, _signInManager, _userManager); if (x.Result == false) return false; else return true; } public static async Task<bool> isSysAdmin(this IPrincipal _principal, SignInManager<ApplicationUser> _signInManager, UserManager <ApplicationUser> _userManager) { var ci = _principal.Identity as ClaimsIdentity; var userName = ci != null ? ci.FindFirst(ClaimTypes.Name) : null; string username = userName?.Value; // get ApplicationUser var appUser = await _userManager.FindByNameAsync( username); var _userClaims = await _signInManager.ClaimsFactory.CreateAsync(appUser); if (_userClaims.UserHasThisPermission(Permissions.AccessAll)) return true; else return false; } public static bool HasRole( this IPrincipal _principal, string roleName, SignInManager<ApplicationUser> _signInManager, UserManager <ApplicationUser> _userManager, ApplicationDbContext _dbContext) { var x = hasrole ( _principal , roleName , _signInManager , _userManager , _dbContext ) ; if (x.Result == false) return false; else return true; } private static async Task<bool> hasrole ( this IPrincipal _principal, string roleName, SignInManager<ApplicationUser> _signInManager, UserManager <ApplicationUser> _userManager, ApplicationDbContext _dbContext) { if (roleName == null) throw new ArgumentNullException(nameof(roleName)); var ci = _principal.Identity as ClaimsIdentity; var userName = ci != null ? ci.FindFirst(ClaimTypes.Name) : null; string username = userName?.Value; var appUser = await _userManager.FindByNameAsync( username); if (_dbContext.Find<UserToRole>(appUser.Id, roleName) != null) { return true ; } return false ; } }

您可以通过_layout.cshtml

You access like this from _layout.cshtml

@using Microsoft.AspNetCore.Identity @inject ApplicationDbContext dbcontext ; @inject UserManager<ApplicationUser> userManager ; @inject SignInManager<ApplicationUser> signInManager; ;; ;; @if ( this.User.IsSysAdmin ( signInManager , userManager ) ) { <!-- add menu stuff --> } @if ( this.User.HasRole ( signInManager , userManager,dbcontext ) ) { <!-- add menu stuff --> }

似乎有很多东西可以传递,但是可以完成工作.

certainly seems like a lot of stuff to pass around but it gets the job done.

顺便说一句,索赔内容来自 www.thereformedprogrammer/part-7-adding-the-better-asp-net-core-authorization-code-into-your-app/

BTW, the claims stuff is from www.thereformedprogrammer/part-7-adding-the-better-asp-net-core-authorization-code-into-your-app/

Jon Smith编写了一个很棒的应用程序,它具有MIT开源许可证,并允许您在Core 3.0/1中使用角色和权限.这非常复杂,但是他提供了缩小版本的 github/JonPSmith/PermissionsOnlyApp 效果很好.谢谢乔恩.

Jon Smith has written a wonderful app which has an MIT open source license and allows you to use roles and permissions in Core 3.0/1 It is very complex but he provided a scaled down version github/JonPSmith/PermissionsOnlyApp that works well. Thanks Jon.

更多推荐

核心3.0 ApplicationUser始终为空

本文发布于:2023-11-17 08:38:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1609484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:为空   核心   ApplicationUser

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!