asp.net MVC4 カミソリ プロジェクトでロールを使用する方法について混乱しています。2 つの主な違いは何ですか。認証されたユーザーのロールを確認するときに、カスタム ロール プロバイダーに移動するように、authorize 属性を使用してそれを作成するにはどうすればよいですか。それとも私はここで物事を混同していますか?
より具体的に:
「管理者」ロールを持つユーザーが CRUD を実行できる管理コントローラーがあります。私のコントローラーでは、次の属性を適用します。
[Authorize(Roles = "administrator")]
public class OverviewController : Controller
authorize 属性がバックエンドでカスタム Role プロバイダーを使用すると仮定するのは正しいですか? もしそうなら、なぜ私にはうまくいかないのですか?
私のカスタム ロール プロバイダー クラスの一部:
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="" });
}
カスタム ロール プロバイダーからの承認についてチェックされますか?
編集
私の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>
編集Ⅱ
public override bool ValidateUser(string email, string password)
{
string salt = _unitOfWork.UsersRepository.GetSalt(email);
string hashedPassword = Helpers.CreatePasswordHash((password), salt);
return _unitOfWork.UsersRepository.UserIsValid(email, hashedPassword);
}