locked
change roleManager - defaultProvider at RunTime / programmatically ?? RRS feed

  • Question

  • User-977048451 posted

    i like to change the default rolemanager provider  at runtime  .. is this possible ? 

    here is some code from ASP.NET - web.config :

        <roleManager enabled="true" defaultProvider="SqlRoleManager_Cars">
          <providers>
            <clear />
            <add connectionStringName="Conn_String2"
                     applicationName="Assets"
                     name="SqlRoleManager_Assets"
                     type="System.Web.Security.SqlRoleProvider" />


            <add connectionStringName="Conn_String2"
                     applicationName="Cars"
                    name="SqlRoleManager_Cars"
                    type="System.Web.Security.SqlRoleProvider" />


          </providers>
        </roleManager>

    i want to change defaultProvider="SqlRoleManager_Cars"  to   >>  "SqlRoleManager_Assets"  

    how to programmatically select it  ??
    i am using same DB on same hosted server ..  ASP.Net framework 3.5 .

    >>

    as you Notice , i'm using Multiple Applications , Both are using the same hosted DB for membership and role manager ..

    <add connectionStringName="Conn_String2"
                     applicationName="Assets"
                     name="SqlRoleManager_Assets"
                     type="System.Web.Security.SqlRoleProvider" />


            <add connectionStringName="Conn_String2"
                     applicationName="Cars"
                    name="SqlRoleManager_Cars"
                    type="System.Web.Security.SqlRoleProvider" />

    when i loging i set loging control as follows :

    Login1.MembershipProvider = "SqlMembershipProvider_Assets"

    but i can't authenticate a user when loging .. it's un-authenticated all the time  !!!

    BTW , everything works fine .. when using single application !

    >>

    any help would be appreciated ...

    Thank you ..

    Sunday, October 23, 2016 12:46 PM

All replies

  • User1724605321 posted

    Hi momoaal ,

    For example, instead of Roles.IsUserInRole which will check only the default provider, we can go directly to the one you need:

    Roles.Providers["MyProvider"].IsUserInRole("Fullmember").

     Or you could try to create a custom RoleProvider. This role provider is the only provider that needs to be registered in the web.config ,Please refer to below threads :

    http://stackoverflow.com/a/19815656/5751404 

    https://msdn.microsoft.com/en-us/library/8fw7xh74.aspx 

    https://blogs.msdn.microsoft.com/jjameson/2010/12/09/a-custom-sqlroleprovider-for-authenticated-users/ 

    Hope it helps.

    Best Regards,

    Nan Yu

    Monday, October 24, 2016 8:28 AM
  • User-977048451 posted

    Thank you , but this didn't work for me !!

    as you Notice , i'm using Multiple Applications , Both are using the same hosted DB for membership and role manager ..

                     applicationName="Assets"
                     name="SqlRoleManager_Assets"
                  

                     applicationName="Cars"
                    name="SqlRoleManager_Cars"

    when i loging i set loging control as follows :

    Login1.MembershipProvider = "SqlMembershipProvider_Assets"

    but i can't authenticate a user when loging .. it's un-authenticated all the time  !!!

    BTW , everything works fine .. when using single application !


    any help , any ideas  , .. please  !

    Monday, October 24, 2016 10:50 AM
  • User1724605321 posted

    Hi momoaal,

     SqlRoleProvider has the ApplicationName property that could be used to set the name of the application for which to store and retrieve role information. But from this link  :

    The ApplicationName property is not thread safe for multiple writes, and changing the ApplicationName property value can result in unexpected behavior when there are multiple users of an application. We recommend that you avoid writing code that allows users to set the ApplicationName property unless you must.

    Best Regards,

    Nan Yu

    Friday, October 28, 2016 8:21 AM