我正在尝试创建一个登录页面。它应该接受用户和密码(这与预期的工作方式一样),但是密码需要进行散列,这就是问题所在。
启动Blazor应用程序后,我在调用my方法散列输入密码的行中得到"A字段初始化器不能引用非静态字段、方法或属性“错误。
这是我的代码:
@page "/"
@using System.ComponentModel.DataAnnotations
@using System.Security.Cryptography
@using System.Text
@using Microsoft.AspNetCore.Identity
<PageTitle>Login</PageTitle>
<div>
<h1>Welcome back!</h1><br />
<h1>Login here:</h1><br />
<EditForm Model=@login>
<DataAnnotationsValidator />
<ValidationSummary />
<div class="form-group">
<label for="Username">Username:</label>
<InputText @bind-Value=@login.user class="form-control" id="Username" />
</div>
<div class="form-group">
<label for="Password">Password:</label>
<InputText @bind-Value=@login.password class="form-control" id="Password" />
</div>
<input type="submit" value="Authenticate" class="btn btn-primary" />
</EditForm><br />
<p>User: @login.user</p>
<p>password: @login.hashedpassword</p>
</div>
@code
{
public class Login
{
[Required]
public string user = "";
[Required]
public string password = "";
public static string HashString(string input)
{
using (SHA512 sha512Hash = SHA512.Create())
{
//From String to byte array
byte[] source = Encoding.UTF8.GetBytes(input);
byte[] hashbytes = sha512Hash.ComputeHash(source);
string hashedinput = BitConverter.ToString(hashbytes).Replace("-", String.Empty);
return hashedinput;
}
}
public string hashedpassword = HashString(password);
}
Login login = new Login();
}发布于 2022-06-05 14:11:35
我的问题被Paul Sinnema's comment解决了。
试试这个
string hashedpassword => HashString(password);。我认为从Razor引用的属性应该有一个getter。就像string test { get; set; }一样,我同意@topsail的观点,你不需要额外的课程。
https://stackoverflow.com/questions/72507608
复制相似问题