我在 AspNetUsers 表中添加了 DepartmentId 列作为外键,并且在另一个名为的表中,我具有与ForeignKey相同的 DepartmentId > MaterialRequest ,我希望登录用户的部门名称显示在“部门名称”视图中
我的课程
部门
部门编号 部门名称
MaterialRequest
MaterialRequestId 材料名称 DepartmentId
我尝试过的
pil_image_obj = Image.open(self.image.path) # Pass the path
答案 0 :(得分:1)
有一种解决方法,您可以创建索赔并采用以下方式:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
// Add custom user claims here => this.DepartmentId is a value stored in database against the user
userIdentity.AddClaim(new Claim("DepartmentId", this.DepartmentId.ToString()));
return userIdentity;
}
// Your Extended Properties
public int? DepartmentId { get; set; }
}
然后创建一个扩展方法来获取您的departmentId:
namespace App.Extensions
{
public static class IdentityExtensions
{
public static string GetDepartmentId(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("DepartmentId");
// Test for null to avoid issues during local testing
return (claim != null) ? claim.Value : string.Empty;
}
}
}
现在您可以轻松地调用此方法来获取DepartmentId:
var departmentId= User.Identity.GetDepartmentId();