【问题标题】:Getting the List of Users in a Role: ASPNEt MVC获取角色中的用户列表:ASPNEt MVC
【发布时间】:2021-05-24 18:18:17
【问题描述】:

我想获取角色中所有用户的列表。我实际上只需要用户名通知电子邮件地址: 我已经尝试了下面的代码,但似乎我无法访问上下文。我在项目中有两个上下文,一个用于 Identity Sample,另一个是我用于其他模型的上下文。

我可以访问用于其他数据模型的上下文,但无法访问 IdentitySample 的上下文。

我们将不胜感激。

 ApplicationDbContext applicationDbContext = new ApplicationDbContext();
             
                var allusers = applicationDbContext.Users.ToList();
                var MDRoles = allusers.Where(x => x.Roles.Select(role => role.Name).Contains("ManagingDirector")).ToList();

该 role.Name 未被识别为角色表中字段的一部分。

【问题讨论】:

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


    【解决方案1】:

    我创建了这个方法,它根据角色名称返回字符串列表(电子邮件),希望它有用:

    public static List<string> GetUsersByRole(string NombreRole)
        {
            var context = new ApplicationDbContext();
    
            List<string> correos = new List<string>();
           
    
            IdentityRole rolevar1 =
                context.Roles.Where(x => x.Name == NombreRole).ToList()[0];
    
            foreach (var usuario in rolevar1.Users)
            {
                correos.Add(context.Users.Find(usuario.UserId).Email);
    
            }
    
            return correos;
        }
    

    【讨论】:

      【解决方案2】:

      我认为您可以使用Any 方法实现此目的

      var MDRoles = allusers.Where(x => 
         x.Roles.Any(role => role.Name == "ManagingDirector")
      ).ToList();
      

      如果这有帮助,请告诉我

      【讨论】:

        【解决方案3】:

        我可以通过编写以下代码来解决:

        ApplicationDbContext applicationDbContext = new ApplicationDbContext();
                    var CORoleID = applicationDbContext.Roles.Where(x => x.Name == "CreditOfficer").FirstOrDefault().Id;
        
                    var UsersInCORole = applicationDbContext.Users.Where(x => x.Roles.Select(role => role.RoleId).Contains(CORoleID)).ToList();
        
                    foreach (var item in UsersInCORole)
                    {
                        string body = string.Empty;
                        var root = AppDomain.CurrentDomain.BaseDirectory; using (var reader = new System.IO.StreamReader(root + @"/EmailTemplates/LoanApprovalStatus.htm"))
                        {
                            string readFile = reader.ReadToEnd();
                            string StrContent = string.Empty;
                            StrContent = readFile;
                            //Assing the field values in the template
                            StrContent = StrContent.Replace("[Firstname]", item.Firstname);
        
                            body = StrContent.ToString();
                        }
        
                        using (var message = new MailMessage(ConfigurationManager.AppSettings["mailAccount"], item.Email))
                        {
        
                            message.Subject = "Request For Loan Submission";
                            message.IsBodyHtml = true;
                            message.Body = body;
        
        
                            // Creatte the credentials:
                            System.Net.NetworkCredential credentials = new NetworkCredential(
                                 ConfigurationManager.AppSettings["mailAccount"],
                                 ConfigurationManager.AppSettings["mailPassword"]
                                 );
        
                            using (SmtpClient client = new SmtpClient
                            {
                                EnableSsl = false,
                                Host = ConfigurationManager.AppSettings["mailHost"],
                                Port = 587,
                                Credentials = credentials
                            })
                            {
                                client.Send(message);
                            }
                        }
        
                    }
                   
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-06-20
          • 2023-04-04
          • 2020-01-14
          • 1970-01-01
          • 1970-01-01
          • 2012-06-30
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多