code_junkie

How to design database for authorization and authentication

Nov 12th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. If user.IsInRole("Admin") Then
  2. deleteButton.Visible = True
  3. else
  4. deleteButton.Visible = False
  5.  
  6. public interface ICustomRole
  7. {
  8. bool IsInRole(string userName, object[] params roles);
  9. }
  10.  
  11. public class MyCustomRole : RoleProvider, ICustomRole
  12. {
  13. public IsInRole(MembershipUser user, object[] params roles)
  14. {
  15. if (roles == null || roles.Length == 0)
  16. throw new ArgumentException("roles");
  17. // Put your logic here for accessing the roles
  18. }
  19. }
  20.  
  21. bool isValid = ((ICustomRole)Roles.Provider).IsInRole(
  22. User, new[] { "Admin", "Moderator", "Validator" });
  23.  
  24. if user.isInRole("Admin") then
  25. deleteButton.visible = true
  26. else
  27. deleteButton.visible = false
  28.  
  29. if user.isInRole("Admin","Moderator") then
  30. deleteButton.visible = true
  31. else
  32. deleteButton.visible = false
  33.  
  34. User(UserID, ...) PK = UserID
  35.  
  36. Role(RoleID, RoleName, ...) PK = RoleID
  37.  
  38. UserHasRole(UserHasRoleID, UserID, RoleID) PK=UserHasRoleID ; Unique= (UserID, RoleID)
  39.  
  40. public class YourSqlRoleProvider : System.Web.Security.RoleProvider
  41. {
  42. private string ConnectionString { get; set; }
  43.  
  44. public override void AddUsersToRoles(string[] userNames, string[] roleNames)
  45. {
  46. // logic here
  47. }
  48.  
  49. public override string ApplicationName
  50. {
  51. get
  52. {
  53. throw new NotSupportedException();
  54. }
  55. set
  56. {
  57. throw new NotSupportedException();
  58. }
  59. }
  60.  
  61. public override void CreateRole(string roleName)
  62. {
  63. throw new NotSupportedException();
  64. }
  65.  
  66. public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
  67. {
  68. throw new NotSupportedException();
  69. }
  70.  
  71. public override string[] FindUsersInRole(string roleName, string userNameToMatch)
  72. {
  73. throw new NotSupportedException();
  74. }
  75.  
  76. public override string[] GetAllRoles()
  77. {
  78. // logic here
  79. }
  80.  
  81. public override string[] GetRolesForUser(string userName)
  82. {
  83. // logic here
  84. }
  85.  
  86. public override string[] GetUsersInRole(string roleName)
  87. {
  88. throw new NotSupportedException();
  89. }
  90.  
  91. public override bool IsUserInRole(string userName, string roleName)
  92. {
  93. return GetRolesForUser(userName).Contains(roleName);
  94. }
  95.  
  96. public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
  97. {
  98. this.ConnectionString = ConfigurationManager.ConnectionStrings[config["connectionStringName"]].ConnectionString;
  99.  
  100. base.Initialize(name, config);
  101. }
  102.  
  103. public override void RemoveUsersFromRoles(string[] userNames, string[] roleNames)
  104. {
  105. throw new NotSupportedException();
  106. }
  107.  
  108. public override bool RoleExists(string roleName)
  109. {
  110. throw new NotSupportedException();
  111. }
  112. }
  113.  
  114. <?xml version="1.0" encoding="utf-8"?>
  115. <configuration>
  116. <connectionStrings>
  117. <clear />
  118. <add name="YourConnectionString" providerName="System.Data.SqlClient" connectionString="connection string here" />
  119. </connectionStrings>
  120. <system.web>
  121. <roleManager defaultProvider="YourSqlRoleProvider" enabled="true">
  122. <providers>
  123. <clear />
  124. <add name="YourSqlRoleProvider" type="YourSqlRoleProvider" connectionStringName="YourConnectionString" />
  125. </providers>
  126. </roleManager>
  127. </system.web>
  128. </configuration>
Add Comment
Please, Sign In to add comment