首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Blazor Server应用程序有问题

Blazor Server应用程序有问题
EN

Stack Overflow用户
提问于 2022-06-05 13:11:13
回答 1查看 73关注 0票数 0

我正在尝试创建一个登录页面。它应该接受用户和密码(这与预期的工作方式一样),但是密码需要进行散列,这就是问题所在。

启动Blazor应用程序后,我在调用my方法散列输入密码的行中得到"A字段初始化器不能引用非静态字段、方法或属性“错误。

这是我的代码:

代码语言:javascript
复制
@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();
    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-05 14:11:35

我的问题被Paul Sinnema's comment解决了。

试试这个string hashedpassword => HashString(password);。我认为从Razor引用的属性应该有一个getter。就像string test { get; set; }一样,我同意@topsail的观点,你不需要额外的课程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72507608

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档