本文介绍了如何使用自定义RoleProvider设置默认角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个用户表,User_In_Roles和角色表.
我创建一个自定义的RoleProvider.注册时,您可以给我示例代码片段,如何将注册时的用户设置为NewUser的默认角色.

RoleProvider的名称为RoleManager.


I create a user table, User_In_Roles and role table.
I create a custom RoleProvider. On registrations can you give me sample code snippet how to set user on registration to the default role of NewUser.

RoleProvider name is RoleManager.


using System;
using System.Collections.Generic;
using System.Linq;
using eegscreening.Models.ViewModels;
using eegscreening.Models.DB;
using System.Web.Security;


namespace test.Models.ObjectManager
{

     public class RoleManager : RoleProvider
     {
          public override string[] GetRolesForUser(string username)
          {
               if (username != null)
               {
                    using (EEGScreeningEntities db = new EEGScreeningEntities())
                   
                         {
                              var user = db.aspnet_Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase) || u.Email.Equals(username, StringComparison.CurrentCultureIgnoreCase));

                              var roles = from ur in user.aspnet_Roles
                                          from r in db.User_In_Roles
                                          where ur.RoleId == r.RoleId
                                          select ur.RoleName;
                         
                              if (roles != null)
                                   return roles.ToArray();
                              else
                                   return new string[] { }; 
                        }
               
               }
               return new string[] { }; 
          }

          public override bool IsUserInRole(string username, string roleName)
          {
               using (EEGScreeningEntities db = new EEGScreeningEntities())
               {
                    var user = db.aspnet_Users.FirstOrDefault(u => u.UserName.Equals(username, StringComparison.CurrentCultureIgnoreCase) || u.Email.Equals(username, StringComparison.CurrentCultureIgnoreCase));

                    var roles = from ur in user.aspnet_Roles
                                from r in db.aspnet_Roles
                                where ur.RoleId == r.RoleId
                                select r.RoleName;
                    if (user != null)
                         return roles.Any(r => r.Equals(roleName, StringComparison.CurrentCultureIgnoreCase));
                    else
                         return false;
               }
          }



          public override void AddUsersToRoles(string[] usernames, string[] roleNames)
          {
               throw new NotImplementedException();
          }

          public override string ApplicationName
          {
               get
               {
                    throw new NotImplementedException();
               }
               set
               {
                    throw new NotImplementedException();
               }
          }

          public override void CreateRole(string roleName)
          {
               throw new NotImplementedException();
          }

          public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
          {
               throw new NotImplementedException();
          }

          public override string[] FindUsersInRole(string roleName, string usernameToMatch)
          {
               throw new NotImplementedException();
          }

          public override string[] GetAllRoles()
          {
               throw new NotImplementedException();
          }

          public override string[] GetUsersInRole(string roleName)
          {
               throw new NotImplementedException();
          }

          public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
          {
               throw new NotImplementedException();
          }

          public override bool RoleExists(string roleName)
          {
               throw new NotImplementedException();
          }
     }

}

推荐答案


这篇关于如何使用自定义RoleProvider设置默认角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 05:48