I´ve created a Blazor Server App with the Microsoft Identity Platform with this tutorial: Blazor Server App Authentication with Azure AD
My application should be a small tool of the company, which is used purely internally. Users will be able to create and manage events. Since it is really only used by a handful of people, I planned to store the name or email addresses of the users in the database to display the correct events of the logged in person based on the user id of the database. To not have to deal with Azure AD any further.
To make this possible, every time someone logs in, I have to query if this email address is already in the database and if not, create a new entry.
My question is where is the best place to do this query.
My first idea was to do this in LoginDisplay.razor
<AuthorizeView>
<Authorized>
<a href="MicrosoftIdentity/Account/SignOut">Log out</a>
Hello, @context.User.Identity?.Name!
</Authorized>
<NotAuthorized>
<a href="MicrosoftIdentity/Account/SignIn">Log in</a>
</NotAuthorized>
</AuthorizeView>
Here I just could add a method that executes, when someone is logged in.
context.User.Identity?.Name
gives me the email address that I need.
But if I put this inside of an OnInitializedAsync(), every time I load a page, the check would happen.
With this code on this page: ASP.NET Core Blazor authentication and authorization, I can get the email address everywhere else.
Where would be the best place to query the user?
CodePudding user response:
I think that you can do it in _Host.cshtml.cs
:
using Microsoft.AspNetCore.Mvc.RazorPages;
public void Onget(){
var email= Request.HttpContext.User.Identity.Name;
//Your query to the database
}