umair mohsin

umair mohsin

  • 1k
  • 387
  • 65.7k

how to set roles in mvc

Mar 30 2021 12:26 PM
i have two different tables in my application named user one and user two.i want to stop user one users to see users two index page and vice versa.how may i apply this customrole.both index pages are protected with [Authorize(roles="one/two")].
 
when debugging moves to customprovider.cs one becomes null and other found that null refrence exception i could not handle.please help me.
 
here is my customprovider.cs code
  1. public override void AddUsersToRoles(string[] usernames, string[] roleNames)  
  2. {  
  3. throw new NotImplementedException();  
  4. }  
  5. public override string ApplicationName  
  6. {  
  7. get  
  8. {  
  9. throw new NotImplementedException();  
  10. }  
  11. set  
  12. {  
  13. throw new NotImplementedException();  
  14. }  
  15. }  
  16. public override void CreateRole(string roleName)  
  17. {  
  18. throw new NotImplementedException();  
  19. }  
  20. public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)  
  21. {  
  22. throw new NotImplementedException();  
  23. }  
  24. public override string[] FindUsersInRole(string roleName, string usernameToMatch)  
  25. {  
  26. throw new NotImplementedException();  
  27. }  
  28. public override string[] GetAllRoles()  
  29. {  
  30. throw new NotImplementedException();  
  31. }  
  32. public override string[] GetRolesForUser(string username)  
  33. {  
  34. DBEntities db = new DBEntities();  
  35. string s = db.user1.Where(x => x.Email == username).FirstOrDefault().UserRole;  
  36. string s1 = db.user2.Where(x => x.Email == username).FirstOrDefault().UserRole;  
  37. if (s == null)  
  38. {  
  39. string[] res1 = { s1 };  
  40. try  
  41. {  
  42. return res1;  
  43. }  
  44. catch (Exception e)  
  45. {  
  46. e.ToString();  
  47. }  
  48. return res1;  
  49. }  
  50. else  
  51. {  
  52. string[] res1 = { s };  
  53. try  
  54. {  
  55. return res1;  
  56. }  
  57. catch (Exception e)  
  58. {  
  59. e.ToString();  
  60. }  
  61. return res1;  
  62. }  
  63. }  
  64. public override string[] GetUsersInRole(string roleName)  
  65. {  
  66. throw new NotImplementedException();  
  67. }  
  68. public override bool IsUserInRole(string username, string roleName)  
  69. {  
  70. throw new NotImplementedException();  
  71. }  
  72. public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)  
  73. {  
  74. throw new NotImplementedException();  
  75. }  
  76. public override bool RoleExists(string roleName)  
  77. {  
  78. throw new NotImplementedException();  
  79. }  

Answers (2)