C# 角色提供程序和System.Web.Security.Roles
我对如何在asp.net MVC4 razor项目中使用角色感到困惑。 这两者之间的区别是什么?主要是,我如何使用authorize属性并使其在检查经过身份验证的用户的角色时转到我的自定义角色提供程序。还是我把事情搞混了 更具体的: 我有一个管理员控制器,角色为“管理员”的用户可以执行CRUD操作。 在控制器中,我应用以下属性:C# 角色提供程序和System.Web.Security.Roles,c#,asp.net-mvc-4,authorization,C#,Asp.net Mvc 4,Authorization,我对如何在asp.net MVC4 razor项目中使用角色感到困惑。 这两者之间的区别是什么?主要是,我如何使用authorize属性并使其在检查经过身份验证的用户的角色时转到我的自定义角色提供程序。还是我把事情搞混了 更具体的: 我有一个管理员控制器,角色为“管理员”的用户可以执行CRUD操作。 在控制器中,我应用以下属性: [Authorize(Roles = "administrator")] public class OverviewController : Controller 假
[Authorize(Roles = "administrator")]
public class OverviewController : Controller
假设authorize属性将在后端使用my custome角色提供程序是否正确?如果是这样,为什么它对我不起作用
自定义角色提供程序类的一部分:
public sealed class CustomRoleProvider : RoleProvider
{
public override void Initialize(string name, NameValueCollection config)
{
if (config == null) throw new ArgumentNullException("config");
if (name.Length == 0) name = "CustomRoleProvider";
if (String.IsNullOrEmpty(config["description"]))
{
config.Remove("description");
config.Add("description", "Custom Role Provider");
}
//Initialize the abstract base class.
base.Initialize(name, config);
_applicationName = Helpers.GetConfigValue(config["applicationName"], System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
}
public override bool IsUserInRole(string email, string roleName)
{
bool isValid = false;
var usersInRole = _unitOfWork.UsersRepository.Get(uir => uir.email == email && uir.Roles.Name == roleName);
if (usersInRole != null) isValid = true;
return isValid;
}
我在做什么?当用户通过这样的正确身份验证时,他/她如何能够:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult LoginValidate(Authentication authentication, string returnUrl)
{
string email = authentication.email;
string password = authentication.password;
bool rememberMe = authentication.rememberMe;
if(string.IsNullOrEmpty(returnUrl)) returnUrl = "/";
//If the filled in fields are validated against the attributes
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(email, password))
{
FormsService.SignIn(email, rememberMe);
return RedirectToAction("Index", "Home", new { area="" });
}
ModelState.AddModelError("", Resources.Resources.Error_incorrect_emailPassword);
}
// Add the ModelState dictionary to TempData here.
TempData["ModelState"] = ModelState;
return RedirectToAction("index", "Home", new { area="" });
}
是否从我的自定义角色提供程序检查他或她的授权
编辑
My web.config:
<roleManager enabled="true" defaultProvider="CustomRoleProvider" cacheRolesInCookie="true" >
<providers>
<clear />
<add name="CustomRoleProvider" type="ArtWebShop.Common.CustomRoleProvider" connectionStringName="ArtWebshopEntities" applicationName="/" />
</providers>
</roleManager>
<membership defaultProvider="CustomMembershipProvider">
<providers>
<clear />
<add name="CustomMembershipProvider" type="ArtWebShop.Common.CustomMembershipProvider" connectionStringName="ArtWebshopEntities" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="0" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
假设authorize属性将使用我的
后端中的自定义角色提供程序
对
如果是这样,为什么它对我不起作用
您可能忘记在web.config中注册此自定义角色提供程序,并将其作为此应用程序的默认提供程序:
<roleManager defaultProvider="CustomRoleProvider" enabled="true">
<providers>
<clear />
<add
name="CustomRoleProvider"
type="Somenamespace.CustomRoleProvider"
/>
</providers>
</roleManager>
假设authorize属性将使用我的
后端中的自定义角色提供程序
对
如果是这样,为什么它对我不起作用
您可能忘记在web.config中注册此自定义角色提供程序,并将其作为此应用程序的默认提供程序:
<roleManager defaultProvider="CustomRoleProvider" enabled="true">
<providers>
<clear />
<add
name="CustomRoleProvider"
type="Somenamespace.CustomRoleProvider"
/>
</providers>
</roleManager>
啊,不,忘了提一下,web.config应该是正确的。我的结构和你们提供的一样。请参阅编辑。然后这应该可以工作。另外,请确保您已经重写了GetRolesForUser
方法。因此,如果我在自定义角色提供程序中的方法IsUserInRole处放置断点,并将authorize属性放置在overview类的顶部,它应该停止在那里?是否每次都需要将代码放在控制器顶部的编辑II中?不,当然不需要。Authorize属性使用GetRolesForUser
方法,而不是IsUserInRole
方法。这就是你应该放置断点的地方。[Authorize(Roles=“administrator”)]是否检查经过身份验证的用户是否具有正确的角色?也就是说,返回true或false?啊,不,忘了提到,web.config应该是正确的。我的结构和你们提供的一样。请参阅编辑。然后这应该可以工作。另外,请确保您已经重写了GetRolesForUser
方法。因此,如果我在自定义角色提供程序中的方法IsUserInRole处放置断点,并将authorize属性放置在overview类的顶部,它应该停止在那里?是否每次都需要将代码放在控制器顶部的编辑II中?不,当然不需要。Authorize属性使用GetRolesForUser
方法,而不是IsUserInRole
方法。这就是你应该放置断点的地方。[Authorize(Roles=“administrator”)]是否检查经过身份验证的用户是否具有正确的角色?即返回真还是假?