我已经做了一个自定义角色提供程序。在我更新VS和NuGet包之前,一切似乎都正常。但是,当我现在登录时,页面看起来好像被刷新了(或者视图至少被重新加载了)。虽然我确实看到一个cookie被创建了,但是我不会重定向到Index。为什么?
在我的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();
}因此,当我登录时,我不能访问Home/Index,它会将我重定向到Login。登录后也一样。
我的自定义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);
}在此之前,这一切都起作用了(授权也是如此)。
发布于 2015-05-12 11:24:03
我终于发现问题出在哪里了!
表面上看,在重新安装软件包之后,我的web.config被更改了。我所要做的就是将enabled="true“添加到roleManager部分。因此,代码应该如下所示:
<roleManager defaultProvider="MyRoleProvider" enabled="true">
<providers>
<add name="MyRoleProvider" type="project.Authorisation.CustomRoleProvider" />
</providers>
</roleManager>所以表面上看,老虎机是禁用的。我希望这会对其他可能遇到这个问题的人有所帮助!
https://stackoverflow.com/questions/30153250
复制相似问题