我已经制作了自定义角色提供商。在更新我的VS和Nuget包装之前,所有内容似乎都起作用。但是,当我现在登录时,看来页面被刷新(或视图至少重新加载)。我确实看到创建了一个cookie,但是我不会重定向到索引。为什么?
在我的web.config中:
<authentication mode="Forms">
<forms loginUrl="~/Home/Login" timeout="2880" />
</authentication>
<roleManager defaultProvider="MyRoleProvider">
<providers>
<add name="MyRoleProvider" type="project.Authorisation.CustomRoleProvider" />
<remove name="MySQLRoleProvider" />
<add name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/" />
</providers>
</roleManager>
在我的homecontroller中:
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(User user)
{
if (ModelState.IsValid)
{
bool authenticated = userDBController.isAuthorized(user.Nickname, user.Password);
if (authenticated)
{
FormsAuthentication.SetAuthCookie(user.Nickname, false);
return Redirect(Url.Action("Index", "Home"));
}
else
{
ViewBag.Message = "Inlog data is incorrect!";
return View();
}
}
else
{
return View();
}
}
[Authorize(Roles = "ADMIN")]
public ActionResult Index()
{
return View();
}
因此,当我登录时,我无法回家/索引,它会重定向我登录。登录后相同的。
我的自定义Roleprovider现在很简单:
public class CustomRoleProvider : RoleProvider
{
private MainController mainController = MainController.Instance;
private UserDBController userDBController = MainController.Instance.GetUserDBController();
public override string[] GetRolesForUser(string username)
{
return userDBController.getRollen(username);
}
在一切之前,这一切都起作用(也是授权)。
好吧,我终于找到了问题!
似乎在重新安装软件包后,更改了我的Web.config。我要做的就是将启用=" true"添加到rolemanager部分。因此,代码应该看起来像:
<roleManager defaultProvider="MyRoleProvider" enabled="true">
<providers>
<add name="MyRoleProvider" type="project.Authorisation.CustomRoleProvider" />
</providers>
</roleManager>
因此,似乎禁用了Rolemanager。我希望这将帮助其他可能遇到这个问题的人!