[英]Blazor execute code after Login with Microsoft Identity Platform
我使用本教程使用 Microsoft Identity Platform 創建了一個 Blazor Server App: Blazor Server App Authentication with Azure AD
我的應用應該是公司的一個小工具,純內部使用。 用戶將能夠創建和管理事件。 由於它真的只有少數人使用,我計划將用戶的姓名或 email 地址存儲在數據庫中,以根據數據庫的用戶 ID 顯示登錄人員的正確事件。 不必再處理 Azure AD。
為了實現這一點,每次有人登錄時,我都必須查詢這個 email 地址是否已經在數據庫中,如果不存在,則創建一個新條目。
我的問題是哪里是執行此查詢的最佳位置。
我的第一個想法是在 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>
在這里,我可以添加一個在有人登錄時執行的方法。context.User.Identity context.User.Identity?.Name
為我提供了我需要的 email 地址。 但是如果我把它放在 OnInitializedAsync() 中,每次加載頁面時,都會進行檢查。
有了這個頁面的代碼: ASP.NET Core Blazor 認證授權,我在其他地方都可以拿到email地址。
查詢用戶的最佳位置在哪里?
我認為你可以在_Host.cshtml.cs
中做到這一點:
using Microsoft.AspNetCore.Mvc.RazorPages;
public void Onget(){
var email= Request.HttpContext.User.Identity.Name;
//Your query to the database
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.