如何在ASP中获取用户信息.. NET Core 3.1从Azure AD B2C用户商店?



使用Visual Studio新项目向导创建了。NET Core 3.1应用程序。大多数默认选项被使用,但是身份验证被配置为使用存储在Azure AD B2C中的个人用户帐户。

web应用程序是由Visual Studio搭建的,编译和运行得很好。登录时,使用@User.Identity.Name将当前用户名打印在标题中。

在摆弄了@User.Identity.Name之后,不清楚如何访问其余的用户信息或"声明";存储在AAD B2C中

在MVC控制器中访问存储在AAD B2C中的用户信息的正确/建议方法是什么?

你可以在类中使用依赖注入注入UserManager。下面是一个例子:

using ContactManager.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace ContactManager.Pages.Contacts
{
public class DI_BasePageModel : PageModel
{
protected ApplicationDbContext Context { get; }
protected IAuthorizationService AuthorizationService { get; }
protected UserManager<IdentityUser> UserManager { get; }
public DI_BasePageModel(
ApplicationDbContext context,
IAuthorizationService authorizationService,
UserManager<IdentityUser> userManager) : base()
{
Context = context;
UserManager = userManager;
AuthorizationService = authorizationService;
} 
}
}

并且,您可以使用FindByNameAsyncFindByIdAsync来获取用户

你可以在。net Core 3.1 Identity上学习这个优秀的材料.

最新更新