c# - 'System.Web.HttpApplication.User' 是一个 'property' 但像 'type' 一样使用

标签 c# asp.net-mvc asp.net-mvc-3 roleprovider user-roles

我是 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”是一个“属性”,但像“类型”一样使用

我用谷歌搜索并尝试了我在 Stack 上找到的链接,但没有任何内容适合我的问题。我只是想将角色从我的自定义数据库表中移到 MVC 的角色中,这样我就可以在某些窗口上使用权限。

有没有人有任何建议或想把我推向正确的方向?

最佳答案

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

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

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

MyNamespace.User user = ...

关于c# - 'System.Web.HttpApplication.User' 是一个 'property' 但像 'type' 一样使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30359784/

相关文章:

c# - 使用来自 $http post 服务的模型重定向到 MVC View

asp.net - 无法在文本框中键入 double

c# - 为什么实例化一个对象不自动实例化它的属性?

c# - 在 ASP.NET MVC 中编写 XML 文件?

javascript - 获取 MVC 模型数据并将其传递到 AngularJS Controller

javascript - 允许 MVC 将 Controller 操作响应视为静态内容

jquery - 加载 AJAX 的 PartialView 和非 AJAX 请求的 View

c# - 复数瑞典语单词?

c# - 如何在 XAML 中动态访问元素名称?

c# - 当 IsReadOnly 是接口(interface)成员时,List<T> 如何使 IsReadOnly 私有(private)?