我想确保我在这个正确的轨道上。
我有一个站点,它有两个后端区域,一个用于学校,一个用于管理。我想对这些区域进行身份验证,以便学校区域的数据库权限少于管理员。为此,我的想法是在 Sql Server Management Studio School 和 Admin 中有两个登录名,我可以将某些角色映射到它们。也许学校部分只有读取权限,而管理员读取和写入等。
请有人告诉我实施此操作的最佳方法。我是否需要多个连接字符串(一个用于管理员,一个用于学校)?这可以使用表单身份验证来完成吗?
我目前正在连接到我使用 Sql Server Management Studio 创建的现有数据库,并且已经有一种登录方式,可以设置 FormsAuthentication 并且在我可以将授权属性添加到我的学校后端控制器的意义上,这很有效除非学校已登录,否则将停止显示需要已登录学校的页面。问题实际上是如何使这一点更具体,以便仅允许学校登录名而不是已登录的管理员成员查看该区域因为他们也会设置 FormsAuthentication。
我已经做了很多谷歌搜索,但没有找到任何特定于我的问题的东西,因此这篇文章。
如果需要,我可以生成代码,而不是要求有人为我编写代码,而是对如何解决这种安全模型的理论解释。
在此先感谢您的帮助。
使用自定义角色提供者的工作解决方案
帐户控制器代码(现在一个用于管理员和学校)
[HttpPost]
public ActionResult LogOn(LogOn model, string returnUrl)
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return Redirect(returnUrl);
}
else
{
// Now it's our job to route the user to the correct place. Ask our DB Helper to tell
// us what kind of user they are and route accordingly.
string sPage = "Index";
string sController = "Home";
var Role = DBHelperFunctions.Instance().GetRoleForUser(model.UserName);
switch (Role.role_name)
{
case LanguageSchoolsConstants.m_RoleAdministrator:
{
sController = "AuthorisedAdmin";
}
break;
case LanguageSchoolsConstants.m_RoleSchool:
{
sController = "AuthorisedSchool";
}
break;
}
return RedirectToAction(sPage, sController);
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
上述方法中使用的 DB Helper 函数:
public role GetRoleForUser(string sUserName)
{
// Should only ever have one role...
role Role = (from roles in DBModel.roles
join userrole in DBModel.user_role on roles.role_id equals userrole.role_id
join users in DBModel.users on userrole.user_id equals users.user_id
where users.username == sUserName
select roles).FirstOrDefault();
return Role;
}
Web.config 更改以允许调用角色提供者:
<roleManager defaultProvider="RoleProvider" enabled="true" cacheRolesInCookie="true">
<providers>
<clear />
<add name="RoleProvider" type="namespace.Models.Security.CustomRoleProvider" />
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="Entities" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
我的角色提供者
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
namespace neamsepace.Models.Security
{
public class CustomRoleProvider : RoleProvider
{
public override void AddUsersToRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override void CreateRole(string roleName)
{
throw new NotImplementedException();
}
public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
{
throw new NotImplementedException();
}
public override string[] FindUsersInRole(string roleName, string usernameToMatch)
{
throw new NotImplementedException();
}
public override string[] GetAllRoles()
{
throw new NotImplementedException();
}
public override string[] GetRolesForUser(string username)
{
using (DB db = new DB())
{
string[] RolesForUser = null;
user User = db.users.FirstOrDefault(u => u.username.Equals(username, StringComparison.CurrentCultureIgnoreCase) ||
u.email.Equals(username, StringComparison.CurrentCultureIgnoreCase));
var roles = from dbroles in db.roles
join userroles in db.user_role on dbroles.role_id equals userroles.role_id
join users in db.users on userroles.user_id equals users.user_id
where users.user_id == User.user_id
select dbroles.role_name;
if (roles != null)
{
RolesForUser = roles.ToArray();
}
else
{
RolesForUser = new string[] { };
}
return RolesForUser;
}
}
public override string[] GetUsersInRole(string roleName)
{
throw new NotImplementedException();
}
public override bool IsUserInRole(string username, string roleName)
{
using (DB db = new DB())
{
bool bUserInRole = false;
user User = db.users.FirstOrDefault(u => u.username.Equals(username, StringComparison.CurrentCultureIgnoreCase) ||
u.email.Equals(username, StringComparison.CurrentCultureIgnoreCase));
var roles = from dbroles in db.roles
join userroles in db.user_role on dbroles.role_id equals userroles.role_id
join users in db.users on userroles.user_id equals users.user_id
where users.user_id == User.user_id
select dbroles.role_name;
if (User != null)
{
bUserInRole = roles.Any(r => r.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
}
return bUserInRole;
}
}
public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
{
throw new NotImplementedException();
}
public override bool RoleExists(string roleName)
{
throw new NotImplementedException();
}
}
}
使用授权的控制器。
[Authorize(Roles = LanguageSchoolsConstants.m_RoleAdministrator)]
public class AuthorisedAdminController : Controller
{
//
// GET: /AuthorisedAdmin/
public ActionResult Index()
{
return View();
}
}
我真的希望这可以帮助任何人,请随时发表评论!
感谢你的帮助。