首页 > 解决方案 > 通过表单授权更改数据库

问题描述

如何通过创建特定用户和密码以允许他们进行更改,从而通过 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 进行更改,如果没有填写凭据,则应该什么也没有发生。

如果您可以建议或推荐某种针对此类授权的特定教程,我什至会很高兴。

标签: c#sql-serverasp.net-mvcasp.net-mvc-5

解决方案


这是我用过的一个例子

获取请求

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", ""));


推荐阅读