【问题标题】:'System.Web.HttpApplication.User' is a 'property' but is used like a 'type''System.Web.HttpApplication.User' 是一个“属性”,但用作“类型”
【发布时间】: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


【解决方案1】:

编译器与 HttpApplication 的 User 属性和 User 类冲突。您可以改用var 来避免冲突:

var user = entities.KeyFobUsers.Single(u => u.username == username);

或者用它所在的命名空间完全限定 User 类:

MyNamespace.User user = ...

【讨论】:

  • 我以为代码在设置用户角色,我将其更改为 var 并没有任何反应。角色永远不会设置,我无法访问该页面。它在尝试捕获,但没有抛出任何错误。我希望我更了解 C#
  • 我会将它作为一个新问题发布并包含编译的代码。
  • 你不会用这种方式来设置用户角色吗?
  • 我认为这是一个不同的问题。您的 first 问题是关于编译器错误的。 下一个 问题是“为什么这不能正确设置“用户”。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-22
  • 2016-10-17
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
相关资源
最近更新 更多