你好,我是有点在这里失去了,我不知道要解决什么:自定义角色提供
的问题是,我的应用程序仍然没有检测到角色分配。 就像我有类似 [Authorize(Roles="user")]
。它不会检测到我的登录信息,也不会允许我继续这种观点。
我一直在关注一个坏的教程,并在这里就是我有这么远: (我想让它先上至少之前我移动到另一个)
这里是我的数据库表:
我没有把任何哈希但为了简单:
登录表
角色表
我用inner join
,我将有这样的事情
select A.username, A.role from login A INNER JOIN roles B on A.role = B.id
我使用代码第一EF 4.0,这里是我的代码片段:
我有一个类roleprovider
,它继承自roleprovider
。
我只执行2种方法从中,即:GetRolesForUser
和IsUserInRole
GetRolesForUser
public override string[] GetRolesForUser(string uname)
{
if (!HttpContext.Current.User.Identity.IsAuthenticated)
{
return null;
}
var cacheKey = string.Format("{0_role}", uname);
if (HttpRuntime.Cache[cacheKey] != null)
{
return (string[])HttpRuntime.Cache[cacheKey];
}
string[] roles = new string[] { };
using (EmployeeContext emp = new EmployeeContext())
{
roles = (from a in emp.login
join b in emp.roles on a.role equals b.id
where a.username.Equals(uname)
select b.role).ToArray<string>();
if (roles.Count() > 0)
{
HttpRuntime.Cache.Insert(cacheKey, roles, null, DateTime.Now.AddMinutes(_cacheTimeoutInMinute), Cache.NoSlidingExpiration);
}
}
return roles;
}
的isUserInRole
public override bool IsUserInRole(string uname, string roleName)
{
var userRoles = GetRolesForUser(uname);
return userRoles.Contains(roleName);
}
的Web.Config
<roleManager>
<providers>
<clear/>
<add name="roleprovider" type="MvcApplication6.Helper.roleprovider"/>
</providers>
</roleManager>
我道歉,如果我不能很好地解释代码的,因为现在我仍然在学习它的过程。到目前为止,我的主要议程是让代码首先工作,但我不知何故丢失了,因为我不确定我错过了什么。
问题概要: - 应用程序不会检测数据库中的角色,如果我尝试登录,不会让我继续。
编辑:这里是我的登录代码(我有身份验证模式实现)
[HttpGet]
[ActionName("login")]
public ActionResult login_load()
{
return View();
}
[HttpPost]
[ActionName("login")]
public ActionResult login_post(string uname,string pword)
{
using (EmployeeContext emp = new EmployeeContext())
{
int success = emp.login.Where(x => x.username == uname && x.password == pword).Count();
if (success == 1)
{
FormsAuthentication.SetAuthCookie(uname, false);
return RedirectToAction("Details", "Enrollment");
}
return View();
}
}
“应用程序不会检测到数据库中的角色”有点含糊。究竟会发生什么? –
即使我拥有[Authorize(Roles =“users”)],我的当前登录角色是'users',它也不会让我继续。让我编辑我的帖子。 –