在Blazor Server应用程序上有问题
我正在尝试创建一个登录页面。它应该接受用户和密码(就像预期的那样起作用),但是需要哈希密码,这是问题所在。
启动Blazor应用程序后,我将获得“字段初始化器无法引用非静态字段,方法或属性”错误,该行中称我的方法调用了我的方法来哈希输入密码。
这是我的代码:
@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 @[email protected] class="form-control" id="Username" />
</div>
<div class="form-group">
<label for="Password">Password:</label>
<InputText @[email protected] 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();
}
I am trying to create a login page. It should accept user and password (which works like intended), but the password needs to be hashed which is the problem.
After starting the Blazor app, I get the "A field initializer cannot reference the non-static field, method, or property" error at the line which calls my method to hash the inputted password.
Here is my code:
@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 @[email protected] class="form-control" id="Username" />
</div>
<div class="form-group">
<label for="Password">Password:</label>
<InputText @[email protected] 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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的问题通过 Paul Sinnema ' 72507608/havith-a-a-problem-blazor-server-application/72508039#comment128085727_72507608“> comment
My problem was solved by Paul Sinnema's comment