繁体   English   中英

从 ASP.NET/Blazor 服务器中的当前经过身份验证的用户获取数据

[英]Get data from the current authenticated user in ASP.NET/Blazor Server

我有这个 ApplicationUser class

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 数据。

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

        [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);

        }

这个 function 返回当前认证用户的 ID。 现在,在我的项目中,我有一个UserService设置。 所以我可以很容易地调用:

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM