【发布时间】:2015-08-02 07:08:26
【问题描述】:
我是 MVC3 的新手,我正在尝试实现客户用户和角色身份验证。我能够使用我的自定义数据库验证用户,但现在我想在同一个表中使用我的自定义角色。我按照以下链接中的说明进行了操作:
MVC Custom Roles and Validation
教程让我将这段代码放入我的 global.asax 文件中。
protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
{
if (FormsAuthentication.CookiesSupported == true)
{
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
try
{
//let us take out the username now
string roles = string.Empty;
string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;
using (RecipeEntities entities = new RecipeEntities())
{
**-->> User user = entities.KeyFobUsers.Single(u => u.username == username);
roles = user.AccessLevel.Trim();
}
//let us extract the roles from our own custom cookie
//Let us set the Pricipal with our user specific details
e.User = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
}
catch
{
//something went wrong
}
}
}
}
目前在 MVC 上面的脚本中,“用户”下划线表示 'System.Web.HttpApplication.User' is a 'property' but is used like a 'type'
我已经四处搜索并尝试了我在 Stack 上找到的链接,但没有任何内容适合我的问题。我只是想把我的自定义数据库表中的角色放到 MVC 的角色中,这样我就可以在某些窗口上使用权限。
有没有人有任何建议或想把我推向正确的方向?
【问题讨论】:
-
当前类必须有一个名为
User的属性,因此您会遇到名称冲突。尝试指定类型的完整命名空间(假设您有一个名为User的类):MyApp.MyNamespace.User user = ...
标签: c# asp.net-mvc asp.net-mvc-3 roleprovider user-roles