【问题标题】:Authorize Attribute always returning false ASP.net MVC Identity授权属性总是返回错误的 ASP.net MVC 身份
【发布时间】:2019-09-16 08:06:36
【问题描述】:

当我使用 Authorize roles 属性装饰方法时,它每次都返回 false。我正在尝试限制只有“管理员”角色的用户才能访问管理页面。

  1. 我已验证我当前登录的用户实际上是“管理员”角色。

  2. 我尝试使用自定义授权属性。结果相同。如果需要,我可以添加代码。

  3. 我发现授权属性适用于用户,但不适用于角色。

  4. 我相信这个问题与以下事实在我的应用程序中不起作用有关:

    User.IsInRole("Admin"). 
    

    但是,这个语句确实有效:

    userManager.IsInRole(user.Id, "Admin")
    

这是我的代码:

public class AdminController : Controller
    {

        //[AuthLog(Roles = "Admin")] //Custom authorization attribute
        [Authorize(Roles = "Admin")]
        public ActionResult Users()
        {

            return View();
        }


    }

也许这有助于调试:

  • Microsoft.AspNet.Identity.Core:V.2.1.0
  • Microsoft.AspNet.Identity.EntityFramework:V.2.1.0

我愿意就我可以从我的项目中发布的任何其他内容提出建议,以便更轻松地进行调试。我已经搜索堆栈 2 周了。

更新 1:用户如何登录

// POST: /account/login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(AccountLoginModel viewModel)
    {
        // Ensure we have a valid viewModel to work with
        if (!ModelState.IsValid)
            return View(viewModel);

        // Verify if a user exists with the provided identity information
        var user = await _manager.FindByEmailAsync(viewModel.Email);

        // If a user was found
        if (user != null)
        {
            // Then create an identity for it and sign it in
            await SignInAsync(user, viewModel.RememberMe);

            // If the user came from a specific page, redirect back to it
            return RedirectToLocal(viewModel.ReturnUrl);
        }

        // No existing user was found that matched the given criteria
        ModelState.AddModelError("", "Invalid username or password.");

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

    private async Task SignInAsync(IdentityUser user, bool isPersistent)
    {
        // Clear any lingering authencation data
        FormsAuthentication.SignOut();

        // Create a claims based identity for the current user
        var identity = await _manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);

        // Write the authentication cookie
        FormsAuthentication.SetAuthCookie(identity.Name, isPersistent);
    }

【问题讨论】:

  • 在对用户进行身份验证后,您如何构建您的 Claimsprincipal?
  • @Jinish 请参阅更新 1
  • 您也可以手动创建 FormsAuthenticationTicket。构造函数采用 userdata 参数,您可以在其中添加身份验证时的角色。将其添加到 httpresponse cookie 集合中的帖子。然后你可以使用你的 User.IsInRole

标签: c# asp.net-mvc authorization asp.net-identity identity


【解决方案1】:

FormsAuthentication.SetAuthCookie(identity.Name, isPersistent);

不幸的是,没有存储任何带有身份的角色。因此,当从 cookie 重新创建身份时,您没有任何角色。验证尝试

this.User.IsInRole("Admin")

你会得到false,即使userManager告诉你不是这样。

有多种解决方法。

例如,您可以切换到任何其他身份持久化器,例如 SessionAuthenticationModule,它可以将您的用户名和角色存储在 cookie 中。你可以关注my tutorial

另一种方法是使用显式角色管理器并使用其功能自动将您的角色存储在另一个 cookie 中,与表单身份验证 cookie 分开。这包括配置角色提供者并编写您自己的角色提供者,该角色提供者将成为用户管理器的适配器。

最后,您可以忘记表单身份验证并使用 Identity 的本机方式来发布 cookie,这将涉及在身份验证管理器上调用 SignInAsync

【讨论】:

  • 感谢您抽出宝贵时间调查我的问题。我想使用 Identity 本机方式。你能指点我一些文档吗?
  • 我查看了文档。它概述了 SignInAsync 但我已经有了。我不确定我需要改变什么。是否有任何其他我可以阅读的文档可以给我一些示例或进一步阅读。非常感谢您抽出宝贵时间,并想在解决后给您买杯啤酒。
  • 你不是把你自己的SignInAsync 内部使用FormsAuthentication 与OWIN 使用SignInManager 发出cookie 的方式混淆了吗?
  • 您可能是对的,但是我如何使用 Owin 来发出 cookie。你能举个例子吗?
猜你喜欢
  • 2019-08-10
  • 2013-02-26
  • 2010-11-25
  • 2011-03-12
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多