Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.


C#
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();
          }
     }

}
Posted
Updated 9-Mar-12 12:05pm
v3

1 solution

try this dude..

http://www.brianlegg.com/post/2011/05/09/Implementing-your-own-RoleProvider-and-MembershipProvider-in-MVC-3.aspx

-this greatly helped me customizing my role provider..
(please mark as answered if it helped you :))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900