[英]Custom Role Provider not working - Instant redirect to login page after logging in
I have made a custom Role Provider. 我做了一个自定义的角色提供者。 Before I updated my VS and NuGet packages everything seemed to work. 在更新VS和NuGet软件包之前,一切似乎都可以正常工作。 However, when I login now it looks like the pages gets refreshed (or the View is reloaded atleast). 但是,当我现在登录时,页面似乎刷新了(或者视图至少被重新加载了)。 I do see a cookie is created though, but I will not get redirect to Index. 我确实看到创建了一个cookie,但是我不会重定向到Index。 Why? 为什么?
In my Web.Config: 在我的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>
In my HomeController: 在我的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();
}
So when I logged in I cannot go to Home/Index, it will redirect me to Login anyways. 因此,当我登录时,无论如何我都无法进入“首页/索引”,它将重定向我到“登录”。 Same for after logging in. 登录后也一样。
My Custom RoleProvider is quite simple right now: 我的自定义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);
}
Before everything, this all worked (also the authorization). 在一切之前,这一切都起作用(也包括授权)。
Well I finally found out what was the issue! 好吧,我终于发现了问题所在!
Appearently after reinstalling packages and what not my web.config was changed. 重新安装软件包后,似乎没有更改我的web.config。 All I had to do is add enabled="true" to the roleManager section. 我所要做的就是在角色管理器部分添加enabled =“ true”。 Therefore the code should look like: 因此,代码应如下所示:
<roleManager defaultProvider="MyRoleProvider" enabled="true">
<providers>
<add name="MyRoleProvider" type="project.Authorisation.CustomRoleProvider" />
</providers>
</roleManager>
So appearently the rolemanager is disabled. 因此,似乎已禁用了角色管理器。 I hope this will help other people who might experience this problem! 希望这对可能遇到此问题的其他人有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.