正如標題所述,我想將 IdentityUser 屬性系結到隱藏的表單輸入。或者我可能走錯路了。它正在跟蹤誰創建了一個新專案。我可以訪問身份和屬性。
這是我的表單接受輸入的示例。專案目標結束日期:<input type="datetime-local" @bind="projectObject.TargetEndDate">
這是 id 喜歡發生的事情的一個例子
<input type="hidden" @bind="projectObject.CreatedBy">
(在此處了解 IdentityUser 的一些方法)
這是我的屬性系結。專案專案物件 = 新專案();
string CurrentUser { get; set; }
protected async override Task OnInitializedAsync()
{
var authstate = await GetAuthenticationStateAsync.GetAuthenticationStateAsync();
CurrentUser = authstate.User.Identity.Name;
}
我想將 CurrentUser 系結到 CreatedBy。編碼答案將不勝感激,但我也想閱讀答案。
uj5u.com熱心網友回復:
如果我正確理解您的問題,解決方案應該是這樣的
@code{
Project projectObject = new Project();
protected async override Task OnInitializedAsync()
{
var authstate = await GetAuthenticationStateAsync.GetAuthenticationStateAsync();
projectObject.CreatedBy = authstate.User.Identity.Name;
}
}
所以通過這種方式,每次打開表單并初始化組件時,都會將當前用戶保存在 created by 屬性上。而且您不需要創建輸入隱藏值!希望它對您有所幫助,歡迎使用 StackOverflow!
如果在您的組件中不起作用,請使用IHttpContextAccessor從另一個用戶那里檢查此解決方案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/431745.html
標籤:C# 实体框架 asp.net 身份 西装外套 .net-6.0
下一篇:物體框架不獲取有關系的行
