c# - 通过表单授权更改数据库
问题描述
如何通过创建特定用户和密码以允许他们进行更改,从而通过 ASP.NET MVC5 应用程序中的表单授予对数据库中更改值的访问权限?我的客户不想基于登录来构建解决方案。任何人都可以看到完整的内容(无需登录),但只有当特定用户输入他的凭据(用户名、密码)并点击“设置”按钮时,才能对数据库进行更改,见图。当用户名和密码与 SQL 表中的相同时,我尝试了一些设置基本的“if else”条件等操作,但即使没有在字段中输入凭据也可以完成更改。图片链接:
我的编辑视图页面:
@model Company.Models.SovaGenNastaveni
@{
ViewBag.Title = "Edit";
Layout = "~/Views/Shared/_LayoutPage1.cshtml";
}
<link href="~/Content/styles/EditForms.css" rel="stylesheet" />
<div id="states" class="container-fluid">
<div class="row">
<h4>Current Values</h4>
</div>
<table id="FormEdit" class="table table-bordered">
<tr>
<th>Item name</th>
<td>@Model.settingsName</td>
</tr>
<tr>
<th class="alert alert-primary">State</th>
@if
(Model.State == 101)
{
<td class="alert alert-success"><strong>Active</strong></td>
}
else
{
<td class="alert alert-danger"><strong>InActive</strong></td>
}
</tr>
<tr>
<th>Description of settings</th>
<td>@Model.settingsDesc</td>
</tr>
</table>
<form id="formEdit" action="/SimulationSettings/edit/@Model.ID" method="post">
<div class="row">
<h4>Change state</h4>
</div>
<div class="form-row">
<div class="form-group col-md-5">
<label for="Hodnota">Choose a new <strong>Hodnota</strong></label>
<select class="form-control" id="ChooseState" name="State">
<option value="@Model.State">---Choose---</option>
<option value="101">Active</option>
<option value="102">InActive</option>
</select>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-5">
<label for="Hodnota">Confirm your rights</label>
<input type="text" class="form-control" id="Name" name="Name" placeholder="username" value="" />
<input type="password" class="form-control" id="Password" name="Password" placeholder="password" />
</div>
</div>
<button type="submit" class="btn btn-success">Set</button>
<a class="btn btn-danger" href="/SimulationSettings/Settings">Cancel</a>
</form>
</div>
控制器:
public ActionResult Edit(long id)
{
SiempelLinkedServerEntities db = new SiempelLinkedServerEntities();
SovaGenNastaveni existingSetting = db.SovaGenNastavenis.Where(x => x.ID == id).FirstOrDefault();
return View(existingSetting);
}
[HttpPost]
//[Authorize]???
public ActionResult Edit(SovaGenNastaveni genNastaveni)
{
SiempelLinkedServerEntities db = new SiempelLinkedServerEntities();
SovaGenNastaveni existingSetting = db.SovaGenNastavenis.Where(x => x.ID == genNastaveni.ID).FirstOrDefault();
existingSetting.State = genNastaveni.State;
db.SaveChanges();
return RedirectToAction("Settings", "SimulationSettings");
}
模型是从 SQL Server 设计和生成的(EF Designer,DB-First Approach)
在 SQL Server 中,我有一个包含 3 列的表 = ID、用户名、密码(这可能会用哈希加密)。该解决方案应仅部署在公司的 Intranet 中。不需要必要的安保或安全措施。
简而言之,应该授权编辑页面,当被授予的用户输入他的凭据时,将对 DB 进行更改,如果没有填写凭据,则应该什么也没有发生。
如果您可以建议或推荐某种针对此类授权的特定教程,我什至会很高兴。
解决方案
这是我用过的一个例子
获取请求
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class ClientToDB : MonoBehaviour
{
void Start()
{
StartCoroutine(SendForValues());
}
IEnumerator SendForValues()
{
using (UnityWebRequest www = UnityWebRequest.Get("http://" + StaticValues.ip + "/empires/ClientToDB.php"))
{
yield return www.SendWebRequest();
if (www.downloadHandler.text != "0")
{
Debug.Log(www.downloadHandler.text);
}
else
{
Debug.Log("Sucessfully Sent");
}
}
}
}
邮政
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class RegisterUser : MonoBehaviour
{
public InputField Field;
public void CreateUser()
{
PlayerPrefs.SetString("EmpireName", Field.text);
StartCoroutine(StartRegisterUser());
}
IEnumerator StartRegisterUser()
{
WWWForm form = new WWWForm();
form.AddField("name", PlayerPrefs.GetString("EmpireName", ""));
using (UnityWebRequest www = UnityWebRequest.Post("http://" + StaticValues.ip + "/empires/UserCreateAccount.php", form))
{
yield return www.SendWebRequest();
Debug.Log(www.downloadHandler.text);
if (www.downloadHandler.text[0] != '0')
{
Debug.Log(www.downloadHandler.text);
}
else
{
Debug.Log(www.downloadHandler.text);
Debug.Log("Sucessfully Sent");
PlayerPrefs.SetInt("EmpireCreated", 1);
SceneManager.LoadScene(2);
int id = int.Parse(www.downloadHandler.text.Split('\t')[1]);
PlayerPrefs.SetInt("id", id);
}
}
}
}
我使用 php 来接收请求,以便使用 php 获取信息,您使用 $_POST["KEY"] 并使用 form.AddField("KEY", PlayerPrefs.GetString("EmpireName", ""));
推荐阅读
- python - 如何在pycharm中导入imputer?
- java - 将位图添加到布局时,仅显示 for 循环中的第一张图像
- laravel - 警告:31491450 字节的 POST 内容长度超过第 0 行未知中 8388608 字节的限制
- python - 基于层次匹配的规则引擎
- python - 如何在正则表达式中使组为 NULL
- python - Selenium 找不到元素
- javascript - 如何将距离添加为 200+?
- wordpress - 试图让联系表格 7 发布数据以调试到屏幕
- css - 转换后对齐div的方法?
- php - 在 Laravel 中同步多对多关系时如何防止更改数据透视表主 ID