【发布时间】:2022-02-24 00:42:54
【问题描述】:
正如标题所述,我想将 IdentityUser 属性绑定到隐藏的表单输入。或者我可能走错路了。 它正在跟踪谁创建了一个新项目。我可以访问身份和属性。
这是我的表单接受输入的示例。
项目目标结束日期:
这是 id 喜欢发生的事情的一个例子
<input type="hidden" @bind="projectObject.CreatedBy">
(在此处了解 IdentityUser 的一些方法)
这是我的属性绑定。 项目 projectObject = new Project();
string CurrentUser { get; set; }
protected async override Task OnInitializedAsync()
{
var authstate = await GetAuthenticationStateAsync.GetAuthenticationStateAsync();
CurrentUser = authstate.User.Identity.Name;
}
我想将 CurrentUser 绑定到 CreatedBy。 编码答案将不胜感激,但我也想阅读答案。
【问题讨论】:
-
当组件(或页面)中的变量可以保存您的值时,为什么要隐藏输入?
-
我需要一种方法将 currentUser 引入该值,并将其绑定到 createdBy。反正那是我的目标。
CurrentUser
将在组件中显示当前用户。现在我希望将其绑定到 createdby 属性
-
好的,如果我理解您的要求,出于安全原因,您必须在控制器中设置用户,而不是在 Blazor 中。
-
所以这是我目前的解决方案,但我不确定它是否安全了。项目项目对象 = 新项目();受保护的异步覆盖任务 OnInitializedAsync() { var authstate = await GetAuthenticationStateAsync.GetAuthenticationStateAsync(); projectObject.CreatedBy = authstate.User.Identity.Name;然后将数据库中的 createdby 更改为字符串。那么这不再安全了吗...?
-
不,它不安全,您必须在服务器端设置 projectObject.CreatedBy,示例代码: var user = await UserManager.FindByEmailAsync(HttpContext.User.Identity.Name); project.CreatedBy = user.Id;
标签: c# entity-framework asp.net-identity blazor .net-6.0