如何通过创建特定的用户和密码来允许他们通过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方法)
在SQL Server中,我有一个包含3列的表= ID,用户名,密码(这可能要用哈希加密)。 该解决方案只能部署在公司的Intranet中。不需要必要的安全性。
简而言之,应该授权编辑页面,当授予权限的用户输入其凭据时,将对DB进行更改,如果没有凭据填充则什么也不会发生。
如果您能为这种类型的授权提出建议或推荐某种特定的教程,我也会很高兴。
答案 0 :(得分:0)
这是我使用过的东西的一个例子
获取请求
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接收请求,因此您可以使用$ _POST [“ KEY”]与php获取信息 并使用form.AddField(“ KEY”,PlayerPrefs.GetString(“ EmpireName”,“”));
发送