【问题标题】:Need help implementing simple role management需要帮助实施简单的角色管理
【发布时间】:2023-03-14 14:18:02
【问题描述】:

我在成员资格和角色管理方面有非常基本的需求。我在成员资格方面取得了成功,并且能够使用 [Authorize] 过滤器,但是 IsInRole() 以及随之而来的一切都失败了。我希望有人能指出我哪里出错了。

1) 在控制器上调用登录操作:

[HttpPost]
    public ActionResult LogOn(LoginVM model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (_employeeService.Login(model))
            {
                _employeeService.CreateTicket(model);

                FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);

                if (!String.IsNullOrEmpty(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                else
                {
                    return RedirectToAction("Index", "Home");
                }
            }
            else
            {
                ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

2) CreateTicket(LovinVM 模型)如下所示:

public HttpCookie CreateTicket(LoginVM input)
    {
        string rolesList;
        using (var tx = _session.BeginTransaction())
        {
            Employee employee = _employeeRepository.GetByUsername(input.Username);

            if (employee.Roles.Count >= 0)
            {
                string[] roles = new string[employee.Roles.Count];
                for (int i = 0; i < employee.Roles.Count; i++)
                {
                    roles[i] = employee.Roles.ElementAt(i).Name;
                }

                rolesList = string.Join(",", roles);
            }
            else
            {
                rolesList = string.Empty;
            }

            tx.Commit();
        }

        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, input.Username, DateTime.Now, DateTime.Now.AddMinutes(20), false, rolesList);

        return new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
    }

如果我在创建工单后检查用户是否处于角色中,但在设置身份验证 cookie 之前,我会得到一个真值:

_employeeService.CreateTicket(model);

bool test = User.IsInRole("Role1");

FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);

如果我在 site.master 中添加检查以根据角色显示/隐藏菜单项,则不再为用户列出任何角色。我已经尝试过 Page.User.IsInRole("Role1") 和 HttpContext.Current.User.IsInRole("Role1") 两者都不是真的。我还收到 [Authorize(Roles="Role1")] 过滤器失败。

【问题讨论】:

  • 您是否在 global.asax 中重新处理了 Identity 对象?
  • 你能稍微扩展一下吗?我不清楚你的意思。
  • 这就是答案,但我无法将评论标记为答案。

标签: asp.net asp.net-mvc


【解决方案1】:

我不确定您的会员资格是否基于旧的基本会员资格提供商,但看看我的早期 MVC 项目之一...当用户成功验证时会调用一个方法...

if (MembershipService.ValidateUser(model.UserName, model.Password))
            {
                FormsService.SignIn(model.UserName, model.RememberMe);

虽然我不是会员服务方面的专家,但听起来好像您的用户没有正确登录,一旦他们“登录”,如果您知道我的意思。

表示 FormsService.SignIn() 的行...您的 LogOn 方法中没有类似的内容。

【讨论】:

  • 这就是 if (_employeeService.Login(model)) 出现在我身上的地方。不过,我会仔细检查一下我没有错过那个电话中的任何内容。谢谢。
  • 啊,好吧 - 我猜你的 employeeService.Login 方法覆盖了 FormsService.SignIn() 方法。
【解决方案2】:

在单独的说明中 - 我很好奇您为什么在似乎没有完成任何事务工作时调用事务提交?你得到一个用户,然后创建一个票证(它进入一个 cookie。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-02-24
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
  • 2017-06-07
  • 2021-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多