0
votes

I created this app using the default for blazorwasm template with individual Auth. Below is some of my csproj file.

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>


<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="3.2.0-preview3.20168.3" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.1" />
  <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.1" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.3" />
<PackageReference Include="Microsoft.AspNetCore.ApiAuthorization.IdentityServer" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>

I am trying to retrieve properties from my ApplicationUser which derives from IdentityUser, when a User is logged in of course. I currently am looking at 3 classes(UserManager, SignInManager, and ApplicationUser). Also I extended the ApplicationUser with a property that I want to retrieve. This property is for querying the database for only the records with this property value. In other words, the values from the database table AspNetUsers.

1

1 Answers

0
votes

I have created a service class to handle this in my application. From my controller I setup ApplicationUser user and then called into the service I created user = await this._userServices.GetUserInformation(User) (the capitalized User is the logged-in user)

In the user service class, this is what I have:

public class UserServices : IUserServices
{
    private UserManager<ApplicationUser> _userManager;

    public UserServices(
        UserManager<ApplicationUser> userManager)
    {
        this._userManager = userManager;
    }

    public async Task<ApplicationUser> GetUserInformation(ClaimsPrincipal userClaim)
    {
        var userId = userClaim?.FindFirst("UserId")?.Value; // returns null if no user logged in
        var user = await _userManager.FindByIdAsync(userId);
        return user;
    }
}

After you call this, you can do a null check on your user.

The user information that you get from here, which is what's in your AspNetUsers table, will have the field you need.

Hope this helps.