【问题标题】:Get User Roles with ASP.net Identity and Web API使用 ASP.net 身份和 Web API 获取用户角色
【发布时间】:2016-06-29 23:10:23
【问题描述】:

我目前正在尝试获取给定用户的角色列表,但在将其适应我们正在使用它的上下文时遇到了一些问题。我之前能够使用此 API 函数获得所有可用角色的列表,

[HttpGet]
[Route("GetRoles")]
public async Task<ApiResponse<List<RoleViewModel>>> GetRoles()
{
    try
    {
        //Get Roles
        var roles = await (from r in _db.AspNetRoles
                     select new RoleViewModel { Id = r.Id, Name = r.Name}).ToListAsync();
        return new ApiResponse<List<RoleViewModel>>{ Success = true, Result =  roles };

    }
    catch(Exception ex)
    {
        return new ApiResponse<List<RoleViewModel>> { Success = false, Message = ex.Message };
    }

}

但似乎无法弄清楚我需要投入什么来获取用户的角色列表。我们从现有数据库方法中使用实体框架代码优先,并从这些表中提取。奇怪的是,虽然没有 AspNetUserRoles 表,因为我猜它只是关联了两个表 AspNetUsers 和 AspNetRoles。无论如何,这是有问题的功能,

[HttpGet]
[Route("GetUserRoles")]
public async Task<ApiResponse<List<RoleViewModel>>> GetUserRoles(string userName)
{
    try
    {
        var userRoles = await (_db.AspNetUsers.FirstOrDefault(u => u.UserName == userName).AspNetRoles).ToListAsync();
    }
    catch (Exception ex)
    {
        return new ApiResponse<List<RoleViewModel>> { Success = false, Message = ex.Message };
    }
}

我得到的当前错误是 AspNetRole 不包含 ToListAsync() 的定义。我认为异步的东西让我有点失望。最后是 RoleViewModel 供参考,

public class RoleViewModel
{
    public string Id { get; set; }

    [Required]
    [StringLength(256)]
    public string Name { get; set; }
}

还有 ApiResponse 类,

public class ApiResponse<TResult>
{
    public bool Success { get; set; }
    public string Message { get; set; }
    public TResult Result { get; set; }
}

我觉得应该有一个简单的修复方法,但我就是不能完全理解它是什么。

【问题讨论】:

  • 你有“使用 System.Data.Entity”吗?
  • 是的,这似乎是我注意到的很多用户问题。我刚刚解决了问题并在下面发布了我的代码,谢谢!

标签: c# asp.net asp.net-web-api asp.net-identity asp.net-roles


【解决方案1】:

刚刚找到了我的问题的答案。我缺少的主要是用户管理器的使用,它使事情变得更加容易。然后我只需要将东西放入我已经定义的函数中。这是代码。

[HttpGet]
[Route("GetUserRoles")]
public async Task<ApiResponse<List<RoleViewModel>>> GetUserRoles(string userName)
{
    try
    {
        // Get the user in question
        var aspUser = (from u in _db.AspNetUsers
                       where u.UserName == userName
                       select u).FirstOrDefaultAsync();

        // Check if the user was found
        if (aspUser == null)
        {
            throw new Exception("User was not found");
        }

        // Get the roles associated with that user
        var userRoles = await UserManager.GetRolesAsync(aspUser.Result.Id.ToString());

        // Setup a RoleViewModel list of roles and iterate through userRoles adding them to the list
        List<RoleViewModel> roleList = new List<RoleViewModel>();
        foreach (var u in userRoles)
        {
            var item = new RoleViewModel { Name = u };
            roleList.Add(item);
        }

        return new ApiResponse<List<RoleViewModel>> { Success = true, Result = roleList };
    }
    catch (Exception ex)
    {
        return new ApiResponse<List<RoleViewModel>> { Success = false, Message = ex.Message };
    }
}

【讨论】:

    猜你喜欢
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2021-11-18
    • 1970-01-01
    相关资源
    最近更新 更多