【问题标题】:Get data from the current authenticated user in ASP.NET/Blazor Server从 ASP.NET/Blazor 服务器中的当前经过身份验证的用户获取数据
【发布时间】:2022-01-04 09:06:45
【问题描述】:

我有这个 ApplicationUser 类

using Microsoft.AspNetCore.Identity;

namespace BbcSrUI.Data.Models
{
    public class ApplicationUser : IdentityUser
    {
        public int BrandId { get; set; }
        public bool EnableNewCall { get; set; }
        public bool EnableNewEvent { get; set; }
        public bool EnableCalls { get; set; }
        public bool EnableEvents { get; set; }
    }
}

我想在索引页面访问认证用户的 BrandId 数据。

【问题讨论】:

    标签: c# asp.net-core asp.net-identity blazor-server-side


    【解决方案1】:

    我目前也在使用相同类型的系统。这是我从经过身份验证的用户那里获取“Ap​​plicationUser”信息的方法。 首先,我有这个功能。

            [CascadingParameter]
            private Task<AuthenticationState> authenticationStateTask { get; set; }
            [CascadingParameter]
            public AuthenticationState AuthenticationState { get; set; }
    
            public async Task<Guid> GetUserId()
            {
                AuthenticationState = await authenticationStateTask;
    
                string Id =
                AuthenticationState.User.FindFirstValue(ClaimTypes.NameIdentifier);
    
                return Guid.Parse(Id);
    
            }
    

    此函数返回当前经过身份验证的用户的 ID。 现在,在我的项目中,我有一个UserService 设置。所以我可以轻松调用:

    ApplicationUser user = await userService.GetUserByIdAsync(await GetUserID);
    

    哪一点我可以访问有关当前登录用户的所有信息。

    AuthorName = user.FullName;
    AuthorPhone = user.PhoneNumber;
    AuthorEmail = user.Email;
    

    我不确定你的项目是如何设置的,但我认为它通常也看起来像这样。如果您有任何问题,尽管问!

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-16
      相关资源
      最近更新 更多