问题描述
所以我正在为我的学校做一个项目。我会事先声明,因为我知道从项目的 stackoverflow 等外部帮助中获得不公平的优势绝对不是什么好事,但我真的需要这方面的帮助。
所以我正在用 mvc 做一个 ASP.NET 核心 web 应用程序,我正在尝试创建一个管理门户,它验证提示输入并重定向到管理页面。包括验证和重定向在内的所有其他操作都可以正常工作,但我面临着严重的安全问题。
我会先把我的代码贴在这里,简要介绍一下我到目前为止所做的事情
AdminPortal.cshtml:
@{
ViewData["Title"] = "Admin Portal";
}
<body>
<button onclick="Prompt()" class="btn btn-primary btn-block">Admin access</button>
<script>
function Prompt() {
let Message = prompt("Admin access is strictly restricted for public users.\nPlease maintain a healthy enviroment in this website.\nPlease input the admin password to continue!")
if (Message != "ZdioN1Fo0)(12AsjH") {
window.location.href = "/"
} else {
window.open("AdminPage");
}
}
</script>
</body>
AdminPage.cshtml:
@{
ViewData["Title"] = "Admin Page";
string[] TableHeaders = new string[]
{
"First name","Last name","Email","Phone Number"
};
Layout = "/Views/Shared/_Layout.cshtml";
}
<style>
body{
display:flex;
background:#222831;
align-items:center;
justify-content:center;
height:100vh;
color:sNow;
}
.table{
background:#fff;
max-height:470px;
overflow-y:auto;
Box-shadow:0px 10px 50px 0px rgba(0,0.5);
border-radius:10px;
}
table{
width:100%;
text-align:center;
border-collapse:collapse;
}
table thead th,table tbody td{
padding:15px;
border:none;
font-weight:600;
font-size:14px;
}
table thead th{
background: #1861ac;
color:sNow;
font-size:16px;
position:sticky;
top:-1%;
}
table tbody td {
border-bottom: 1px solid rgba(0,0.1);
}
nav{
display:none !important;
}
</style>
<div class="table">
<table class="table table-bordered table-hover">
<thead>
<tr>
@{
foreach (var head in TableHeaders)
{
<th>
@head
</th>
}
}
</tr>
</thead>
<tbody>
@{
if (Model != null)
{
foreach (var Acc in Model)
{
<tr>
<td>@Acc.Fname</td>
<td>@Acc.Lname</td>
<td>@Acc.Email</td>
<td>@Acc.PhoneNO</td>
</tr>
}
}
}
</tbody>
</table>
</div>
好的,如您所见,管理员密码 ZdioN1Fo0)(12AsjH
对任何在管理门户页面上检查元素的用户都是可见的,这打破了整个仅限管理员访问的概念。我想知道如何将密钥从公众的视野中隐藏起来,并且可能仍然进行验证。
另外,关于安全问题,我想包括另一个功能,它只允许用户通过管理门户页面进入管理页面。我该怎么做?我想过使用返回值进行验证,但不知道该怎么做。
我来之前问过我的朋友,他们说我不应该在前端进行身份验证,而是在后端进行;我不知道;我的 homecontroller.cs 是我的后端吗?不知道如何从那里验证。
我也做了一些研究,认为 https://stackoverflow.com/a/14774470/13734672 在某些方面可能会派上用场,但仍然没有找到实现它的方法。
除了现有的问题之外,我想知道是否可以在重定向 window.location.href = "/"
中添加一个功能,比如另一个提示“未经授权的尝试!”,当重定向到 index.cshtml 时是否可能?
任何帮助将不胜感激:)
解决方法
是的,您的朋友是对的,我们不应该从客户端对用户进行身份验证,因为一切都是在客户端以某种或其他方式在客户端完成的,任何最终用户都可以轻松找到。 ASP.NET 提供了两个功能来实现这一点。一种身份验证,涉及“您是谁?作为您是本网站的有效用户”。其次,授权“您有什么权利在网站上执行任何操作?”
参考:ASP-NET-authentication-and-authorization
由于这是学术项目,我将为您提供具有示例实现的链接 https://www.codeproject.com/Articles/98950/ASP-NET-authentication-and-authorization-2
并回答您关于重定向的最后一部分。一旦您实施了身份验证和授权并配置了哪些用户/角色访问哪些页面,它将重定向到登录页面。如果您希望客户页面重定向,例如 accessdenide 等。您可以使用 web.config