gpt4 book ai didi

c# - 获取 HTML 文本字段并在 C# (cshtml) 中用作变量

转载 作者:行者123 更新时间:2023-11-27 23:05:30 27 4
gpt4 key购买 nike

我有这个 HTML 格式的输入

<input type="text" placeholder="Username" name="usernameField" id="username" runat="server">

我想获取此 textfield 的值并在 cshtml 中的 c# 方法中使用它,我知道我不能使用常规document.getElementById。我试过 HTML agility pack 但据我所知它不能用于此目的。这样做的目的是,我有两个输入,用户名和密码,用户在其中输入登录信息,它会将输入值与数据库中的值进行比较。

我想使用 HTML 元素值的位置在 SQL 语句中,用户名和密码:

WHERE username = " + "'" + username + "'" + " AND password = " + "'" + password

这是完整的 c# cshtml 方法:

@using System;
@using Npgsql;
@using HtmlAgilityPack;
@functions{

public Boolean ValidateLogin()
{

NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;User Id=postgres;
Password=postgres;Database=login;");
// Open Database connection.
conn.Open();

// Define a query returning a single row result set.
NpgsqlCommand command = new NpgsqlCommand("SELECT username, password FROM logindata
WHERE username = " + "'" + username + "'" + " AND password = " + "'" + password + "'" + ";", conn);

// Execute the query and obtain a result set.
NpgsqlDataReader dr = command.ExecuteReader();

// If result exists return true and gain access to the control website.
if (dr.Read())
{
return true;
}

// Close Database connection.
conn.Close();
return false;
}
}

更多信息:

  • 我在 visual studio 2017 中执行此操作
  • 我正在使用 asp.net CORE
  • 我正在使用 cshtml 文件(我相信也称为 razor)

最佳答案

您在此处使用的技术称为 Razor Pages,它是 ASP.NET Core MVC 的一项新功能,可使以页面为中心的场景编码更加轻松和高效。( Razor Pages docs )

如果您正在寻找有关如何使用模型- View - Controller 方法的信息,请查看 here .

对于您的问题,您需要一个 Login.cshtml 和一个 Login.cshtml.cs 文件,如果您右键单击 页面,它们会自动为您创建 文件夹并选择 Add > Razor Page...

这些文件应该包含:

登录.cshtml

@page
@model WebApplication1.Pages.LoginModel

<form method="POST">
<input type="text" placeholder="Username" asp-for="UserName">
<input type="password" asp-for="Password">

<input type="submit" />
</form>

登录.cshtml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace WebApplication1.Pages
{
public class LoginModel : PageModel
{
[BindProperty]
public string UserName { get; set; }

[BindProperty]
public string Password { get; set; }

public bool IsloggedIn { get; private set; }

public void OnPost()
{
NpgsqlConnection conn = new NpgsqlConnection("Server=localhost;User Id=postgres; Password = postgres; Database = login; ");
// Open Database connection.
conn.Open();

// Define a query returning a single row result set.
NpgsqlCommand command = new NpgsqlCommand("SELECT username, password FROM logindata WHERE username = @username AND password = @password", conn);

command.Parameters.AddWithValue("@username", UserName);
command.Parameters.AddWithValue("@password", Password);

// Execute the query and obtain a result set.
NpgsqlDataReader dr = command.ExecuteReader();

// If result exists return true and gain access to the control website.
if (dr.Read())
{
IsloggedIn = true;
}

// Close Database connection.
conn.Close();
}
}
}

请注意,我更改了它们在查询中使用 usernamepassword 的方式,以防止 SQL 注入(inject)攻击。

关于c# - 获取 HTML 文本字段并在 C# (cshtml) 中用作变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49719096/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com