在Blazor Server应用程序上有问题

发布于 2025-02-04 12:03:46 字数 2163 浏览 1 评论 0原文

我正在尝试创建一个登录页面。它应该接受用户和密码(就像预期的那样起作用),但是需要哈希密码,这是问题所在。

启动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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2025-02-11 12:03:46

我的问题通过 Paul Sinnema ' 72507608/havith-a-a-problem-blazor-server-application/72508039#comment128085727_72507608“> comment

尝试此字符串hashedpassword =&gt; hashstring(密码);。我认为从razor引用的属性应该有一个getter。像字符串test {get;放; },我同意@topsail的观点,您不需要额外的类。

My problem was solved by Paul Sinnema's comment

Try this string hashedpassword => HashString(password);. I think a property referenced from Razor should have a getter. Like string test { get; set; }, And I agree with @topsail that you don't need the extra class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文