0

I have created my own custom role provider class "SGI_RoleProvider" and configured properly. Everything is working fine.

Suppose that I have added a public method say "SayHello()", then how can i call that. Because if i am using Roles then the method is not displayed. If i am forcefully using that Roles.SayHello() then compiler gives the error.

Any suggestion how can i call this. Because creating a new instance of SGI_RoleProvider is meaningless.

Thanks for sharing your time.

IrfanRaza
  • 3,030
  • 17
  • 64
  • 89

2 Answers2

3
var myProvider = Roles.Provider as SGI_RoleProvider;
myProvider.SayHello();

Supposing you have correctly defined your provider as the default Provider

uvita
  • 4,124
  • 1
  • 27
  • 23
  • thanks buddy! But even casting does not work here because Roles is a static class. By the way i m using c#. – IrfanRaza Apr 21 '10 at 20:55
  • I tested this code and it works, I am not casting the Roles class but the Provider property of the Roles class which is of type RoleProvider. See http://msdn.microsoft.com/en-us/library/system.web.security.roles.provider.aspx. The code I wrote is C#, maybe you're using framework 2.0, so you can write CustomProvider myProvider = Roles.Provider as CustomProvider; myProvider.SayHello(); instead. Good luck! – uvita Apr 21 '10 at 23:55
  • Great!!! Thanks uvita! It works. Actually I forget to use Provider property. I was casting Roles. Yes I was considering the code in VB. – IrfanRaza Apr 22 '10 at 06:48
  • Thanks Buddy ! have been looking for it Since Ages..:) – Sai Avinash Oct 30 '13 at 13:03
0

Maybe you can do something like

((SGI_RoleProvider)Roles.Provider).SayHello();

One line syntax does it all.

Simon Loh
  • 325
  • 5
  • 15