accessing membership DB from windows (not ASP) app

D

David Thielen

Hi;

When we create the membership DB we have to create the admin user in it in
our setup program. Otherwise, there is no way for an admin to get in our
ASP.NET app and create additional users.

What we have works unless the user first selects the wrong database for
which database is the membership database (this step occurs when they created
the database and then cancel or an error occurs before the admin member is
added).

I think what is happening is that the SqlRoleProvider is not re-reading the
setup app's config file. We update the file as follows before create the
SqlRoleProvider object.

VendorInfo info = new SqlServerInfo();
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection cs =
(ConnectionStringsSection)config.GetSection("connectionStrings");
cs.ConnectionStrings.Remove("MembershipSqlServer");
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
// ConnectionStringSettingsCollection csc =
ConfigurationManager.ConnectionStrings;

cs.ConnectionStrings.Add(new
ConnectionStringSettings("MembershipSqlServer", connStr, info.ClassName));
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
ConnectionStringSettingsCollection csc =
ConfigurationManager.ConnectionStrings;
object obj = csc["MembershipSqlServer"];
cs = (ConnectionStringsSection)config.GetSection("connectionStrings");
csc = cs.ConnectionStrings;
obj = csc["MembershipSqlServer"];

obj is the correct connection string the second time (the first time it's
wrong as I entered it wrong to test this).

The error I get is when I call the roleProvider object to check IsInRole it
tells me it cannot find the stored procedure it uses.

Any ideas how to work around this?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
L

Luke Zhang [MSFT]

Hello Dave,

The connection string property of RoleProvider class is set in its
Initialize() method. And In Initialize() method, it will call
ConfigurationManager's ConnectionStrings to get the actual string. So after
you change the ConnectionStrings in your previous code, you may call
ConfigurationManager's RefreshSection method to refresh the named section
so the next time it is retrieved it will be re-read from disk.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Luke Zhang [MSFT]

Hello Dave,

Do you mean create a singleton RoleProvider? It will still load the
original value of connection string and didn't use the updated one. I think
you may consider create your customized RoleProvider and override the
Initialize() method. For example. like this:

http://msdn2.microsoft.com/en-us/library/317sza4k.aspx


Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Luke Zhang [MSFT]

Hello Dave,

Can you let us know how you implement the customized roleprovider? In fact,
in roleprovider's Initialize method, we can set how it load the connection
string:

public override void Initialize(string name, NameValueCollection config)
{

....
pConnectionStringSettings =
ConfigurationManager.ConnectionStrings[config["connectionStringName"]];

if (pConnectionStringSettings == null ||
pConnectionStringSettings.ConnectionString.Trim() == "")
{
throw new ProviderException("Connection string cannot be blank.");
}

connectionString = pConnectionStringSettings.ConnectionString;
}


From http://msdn2.microsoft.com/en-us/library/317sza4k.aspx

We may change code here to set the connection we want.

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

Is the source to SqlRoleProvider available? The problem appears to be in:

internal static RuntimeConfig GetAppConfig()
{
if (!HttpConfigurationSystem.UseHttpConfigurationSystem)
{
return RuntimeConfig.GetClientRuntimeConfig();
}
return CachedPathData.GetApplicationPathData().RuntimeConfig;
}

After the first request it gets the cached config. How can I have it re-read
it?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
L

Luke Zhang [MSFT]

hello,

The source of SqlRoleProvider is not shared, but we can get some idea from
Reflector:

http://www.aisto.com/roeder/dotnet/

Also, I think you may create the customized RoleProvider by inheirting from
SqlRoleProvider and override its Initialize() method

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

Let me add to that. RefreshSection does not cause it to re-read the entry.
Here is my code:

public static void SetAppExeConfig(string connStr)
{

VendorInfo info = new SqlServerInfo();
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection cs = config.ConnectionStrings;
cs.ConnectionStrings.Remove("MembershipSqlServer");
cs.ConnectionStrings.Add(new
ConnectionStringSettings("MembershipSqlServer", connStr, info.ClassName));
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
}

At the end of that if the above is called twice, the first time with the
wrong connection string, it continues to use the first string.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
D

Dominick Baier

Not sure about your specific scenario - but this works for me:

namespace ConfigTest
{
class Program
{
static void Main(string[] args)
{
UpdateAppSettings();

Console.WriteLine("1");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}

UpdateAppSettings();

Console.WriteLine("2");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}
}

static void UpdateAppSettings()
{
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);

// Add an entry to appSettings.
int appStgCnt =
ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString();

string newValue = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();

config.AppSettings.Settings.Add(newKey, newValue);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");

}
}
}


i guess you have to re-create the provider after you changed the connection
string...
 
D

David Thielen

Hi;

That works for me too. And if I re-read the connection strings - I get the
new ones. The problem is that SqlRoleManager does not re-read the connection
string. From looking at the code through Reflector (what a great program) it
looks like it has it's own cache of the connection string and once read, it
will never re-read it.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Dominick Baier said:
Not sure about your specific scenario - but this works for me:

namespace ConfigTest
{
class Program
{
static void Main(string[] args)
{
UpdateAppSettings();

Console.WriteLine("1");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}

UpdateAppSettings();

Console.WriteLine("2");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}
}

static void UpdateAppSettings()
{
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);

// Add an entry to appSettings.
int appStgCnt =
ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString();

string newValue = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();

config.AppSettings.Settings.Add(newKey, newValue);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);

// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");

}
}
}


i guess you have to re-create the provider after you changed the connection
string...


-----
Dominick Baier (http://www.leastprivilege.com)
Let me add to that. RefreshSection does not cause it to re-read the
entry. Here is my code:

public static void SetAppExeConfig(string connStr)
{
VendorInfo info = new SqlServerInfo();
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
;
ConnectionStringsSection cs = config.ConnectionStrings;
cs.ConnectionStrings.Remove("MembershipSqlServer");
cs.ConnectionStrings.Add(new
ConnectionStringSettings("MembershipSqlServer", connStr,
info.ClassName));
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
}
At the end of that if the above is called twice, the first time with
the wrong connection string, it continues to use the first string.

Cubicle Wars - http://www.windwardreports.com/film.htm
 
D

Dominick Baier

then re-instantiate the provider from scratch after you changed the connection
string


-----
Dominick Baier (http://www.leastprivilege.com)
Hi;

That works for me too. And if I re-read the connection strings - I get
the new ones. The problem is that SqlRoleManager does not re-read the
connection string. From looking at the code through Reflector (what a
great program) it looks like it has it's own cache of the connection
string and once read, it will never re-read it.

Cubicle Wars - http://www.windwardreports.com/film.htm

Dominick Baier said:
Not sure about your specific scenario - but this works for me:

namespace ConfigTest
{
class Program
{
static void Main(string[] args)
{
UpdateAppSettings();
Console.WriteLine("1");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}
UpdateAppSettings();

Console.WriteLine("2");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}
}
static void UpdateAppSettings()
{
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Add an entry to appSettings.
int appStgCnt =
ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString();
string newValue = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();

config.AppSettings.Settings.Add(newKey, newValue);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
}
}
}
i guess you have to re-create the provider after you changed the
connection string...

-----
Dominick Baier (http://www.leastprivilege.com)
Let me add to that. RefreshSection does not cause it to re-read the
entry. Here is my code:

public static void SetAppExeConfig(string connStr)
{
VendorInfo info = new SqlServerInfo();
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.Non
e)
;
ConnectionStringsSection cs = config.ConnectionStrings;
cs.ConnectionStrings.Remove("MembershipSqlServer");
cs.ConnectionStrings.Add(new
ConnectionStringSettings("MembershipSqlServer", connStr,
info.ClassName));
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
}
At the end of that if the above is called twice, the first time with
the wrong connection string, it continues to use the first string.
Cubicle Wars - http://www.windwardreports.com/film.htm

:

yes config is cached - thats how it works....

-----
Dominick Baier (http://www.leastprivilege.com)
That's how I got the above code - from the reflector. It looks
like RuntimeConfig.GetAppConfig() reads the config file the first
time and then always uses a cached config after that.

Is there anyway to tell it to re-read that cached internal config?

Cubicle Wars - http://www.windwardreports.com/film.htm

:

hello,

The source of SqlRoleProvider is not shared, but we can get some
idea from Reflector:

http://www.aisto.com/roeder/dotnet/

Also, I think you may create the customized RoleProvider by
inheirting from SqlRoleProvider and override its Initialize()
method

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default
.a
sp
x#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for
non-urgent
issues where an initial response from the community or a
Microsoft
Support Engineer within 1 business day is acceptable. Please note
that each follow up response may take approximately 2 business
days
as the support professional working with you may need further
investigation to reach the most efficient resolution. The
offering
is
not appropriate for situations that require urgent, real-time or
phone-based interactions or complex project analysis and dump
analysis issues. Issues of this nature are best handled working
with
a dedicated Microsoft Support Engineer by contacting Microsoft
Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers
no
rights.
 
D

David Thielen

I do that. After the code I posted above I call

public static SqlRoleProvider CreateRoleProvider()
{
SqlRoleProvider roleProvider = new SqlRoleProvider();
roleProvider.Initialize("AspNetSqlRoleProvider", ReadConfig(roleConfig));
return roleProvider;
}

private static NameValueCollection ReadConfig(string config)
{
NameValueCollection nvc = new NameValueCollection();
XmlDocument doc = new XmlDocument();
doc.LoadXml(config);
foreach (XmlAttribute attr in doc.DocumentElement.Attributes)
nvc.Add(attr.Name, attr.Value);
return nvc;
}

private static string memberConfig = "<add
name='AspNetSqlMembershipProvider' " +
"connectionStringName='MembershipSqlServer'
enablePasswordRetrieval='false' " +
"enablePasswordReset='true' requiresQuestionAndAnswer='false'
applicationName='/WindwardPortal' " +
"requiresUniqueEmail='false' passwordFormat='Hashed'
maxInvalidPasswordAttempts='5' " +
"minRequiredPasswordLength='5' minRequiredNonalphanumericCharacters='0'
passwordAttemptWindow='10'/>";
private static string roleConfig = "<add " + // no name=
"connectionStringName='MembershipSqlServer'
applicationName='/WindwardPortal'/>";
}

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm




Dominick Baier said:
then re-instantiate the provider from scratch after you changed the connection
string


-----
Dominick Baier (http://www.leastprivilege.com)
Hi;

That works for me too. And if I re-read the connection strings - I get
the new ones. The problem is that SqlRoleManager does not re-read the
connection string. From looking at the code through Reflector (what a
great program) it looks like it has it's own cache of the connection
string and once read, it will never re-read it.

Cubicle Wars - http://www.windwardreports.com/film.htm

Dominick Baier said:
Not sure about your specific scenario - but this works for me:

namespace ConfigTest
{
class Program
{
static void Main(string[] args)
{
UpdateAppSettings();
Console.WriteLine("1");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}
UpdateAppSettings();

Console.WriteLine("2");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}
}
static void UpdateAppSettings()
{
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Add an entry to appSettings.
int appStgCnt =
ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString();
string newValue = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();

config.AppSettings.Settings.Add(newKey, newValue);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
}
}
}
i guess you have to re-create the provider after you changed the
connection string...

-----
Dominick Baier (http://www.leastprivilege.com)
Let me add to that. RefreshSection does not cause it to re-read the
entry. Here is my code:

public static void SetAppExeConfig(string connStr)
{
VendorInfo info = new SqlServerInfo();
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.Non
e)
;
ConnectionStringsSection cs = config.ConnectionStrings;
cs.ConnectionStrings.Remove("MembershipSqlServer");
cs.ConnectionStrings.Add(new
ConnectionStringSettings("MembershipSqlServer", connStr,
info.ClassName));
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
}
At the end of that if the above is called twice, the first time with
the wrong connection string, it continues to use the first string.
Cubicle Wars - http://www.windwardreports.com/film.htm

:

yes config is cached - thats how it works....

-----
Dominick Baier (http://www.leastprivilege.com)
That's how I got the above code - from the reflector. It looks
like RuntimeConfig.GetAppConfig() reads the config file the first
time and then always uses a cached config after that.

Is there anyway to tell it to re-read that cached internal config?

Cubicle Wars - http://www.windwardreports.com/film.htm

:

hello,

The source of SqlRoleProvider is not shared, but we can get some
idea from Reflector:

http://www.aisto.com/roeder/dotnet/

Also, I think you may create the customized RoleProvider by
inheirting from SqlRoleProvider and override its Initialize()
method

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default
.a
sp
x#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for
non-urgent
issues where an initial response from the community or a
Microsoft
Support Engineer within 1 business day is acceptable. Please note
that each follow up response may take approximately 2 business
days
as the support professional working with you may need further
investigation to reach the most efficient resolution. The
offering
is
not appropriate for situations that require urgent, real-time or
phone-based interactions or complex project analysis and dump
analysis issues. Issues of this nature are best handled working
with
a dedicated Microsoft Support Engineer by contacting Microsoft
Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers
no
rights.

 
D

Dominick Baier

Not sure what your problem is - but the following code works for me - and
the private m_connectionString variable reflects the new connection string...

public class Class1
{
static void Main(string[] args)
{
ShowCS();
SqlRoleProvider prov1 = GetProvider();

ChangeConfig();
ShowCS();

SqlRoleProvider prov2 = GetProvider();
}

private static void ShowCS()
{
Console.WriteLine(ConfigurationManager.ConnectionStrings["MyCS"].ConnectionString);
}

private static void ChangeConfig()
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringsSection cs = config.ConnectionStrings;
cs.ConnectionStrings.Remove("MyCS");
cs.ConnectionStrings.Add(new
ConnectionStringSettings("MyCS", "bar", ""));
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");

}

static SqlRoleProvider GetProvider()
{
ProviderSettings ps = new ProviderSettings("Test1",
"System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");

ps.Parameters.Add("connectionStringName", "MyCS");

Type t = typeof(SqlRoleProvider);

ProviderBase pb = ProvidersHelper.InstantiateProvider(ps, t);
return (SqlRoleProvider)pb;

}
}
}

-----
Dominick Baier (http://www.leastprivilege.com)
I do that. After the code I posted above I call

public static SqlRoleProvider CreateRoleProvider()
{
SqlRoleProvider roleProvider = new SqlRoleProvider();
roleProvider.Initialize("AspNetSqlRoleProvider",
ReadConfig(roleConfig));
return roleProvider;
}
private static NameValueCollection ReadConfig(string config)
{
NameValueCollection nvc = new NameValueCollection();
XmlDocument doc = new XmlDocument();
doc.LoadXml(config);
foreach (XmlAttribute attr in doc.DocumentElement.Attributes)
nvc.Add(attr.Name, attr.Value);
return nvc;
}
private static string memberConfig = "<add
name='AspNetSqlMembershipProvider' " +
"connectionStringName='MembershipSqlServer'
enablePasswordRetrieval='false' " +
"enablePasswordReset='true' requiresQuestionAndAnswer='false'
applicationName='/WindwardPortal' " +
"requiresUniqueEmail='false' passwordFormat='Hashed'
maxInvalidPasswordAttempts='5' " +
"minRequiredPasswordLength='5'
minRequiredNonalphanumericCharacters='0'
passwordAttemptWindow='10'/>";
private static string roleConfig = "<add " + // no name=
"connectionStringName='MembershipSqlServer'
applicationName='/WindwardPortal'/>";
}
Cubicle Wars - http://www.windwardreports.com/film.htm

Dominick Baier said:
then re-instantiate the provider from scratch after you changed the
connection string

-----
Dominick Baier (http://www.leastprivilege.com)
Hi;

That works for me too. And if I re-read the connection strings - I
get the new ones. The problem is that SqlRoleManager does not
re-read the connection string. From looking at the code through
Reflector (what a great program) it looks like it has it's own cache
of the connection string and once read, it will never re-read it.

Cubicle Wars - http://www.windwardreports.com/film.htm

:

Not sure about your specific scenario - but this works for me:

namespace ConfigTest
{
class Program
{
static void Main(string[] args)
{
UpdateAppSettings();
Console.WriteLine("1");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}
UpdateAppSettings();
Console.WriteLine("2");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings);
}
}
static void UpdateAppSettings()
{
// Get the configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Add an entry to appSettings.
int appStgCnt =
ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString();
string newValue = DateTime.Now.ToLongDateString() + " " +
DateTime.Now.ToLongTimeString();
config.AppSettings.Settings.Add(newKey, newValue);

// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings");
}
}
}
i guess you have to re-create the provider after you changed the
connection string...
-----
Dominick Baier (http://www.leastprivilege.com)
Let me add to that. RefreshSection does not cause it to re-read
the entry. Here is my code:

public static void SetAppExeConfig(string connStr)
{
VendorInfo info = new SqlServerInfo();
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.N
on
e)
;
ConnectionStringsSection cs = config.ConnectionStrings;
cs.ConnectionStrings.Remove("MembershipSqlServer");
cs.ConnectionStrings.Add(new
ConnectionStringSettings("MembershipSqlServer", connStr,
info.ClassName));
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
}
At the end of that if the above is called twice, the first time
with
the wrong connection string, it continues to use the first string.
Cubicle Wars - http://www.windwardreports.com/film.htm
:

yes config is cached - thats how it works....

-----
Dominick Baier (http://www.leastprivilege.com)
That's how I got the above code - from the reflector. It looks
like RuntimeConfig.GetAppConfig() reads the config file the
first time and then always uses a cached config after that.

Is there anyway to tell it to re-read that cached internal
config?

Cubicle Wars - http://www.windwardreports.com/film.htm

:

hello,

The source of SqlRoleProvider is not shared, but we can get
some idea from Reflector:

http://www.aisto.com/roeder/dotnet/

Also, I think you may create the customized RoleProvider by
inheirting from SqlRoleProvider and override its Initialize()
method

Sincerely,

Luke Zhang

Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/defau
lt
.a
sp
x#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for
non-urgent
issues where an initial response from the community or a
Microsoft
Support Engineer within 1 business day is acceptable. Please
note
that each follow up response may take approximately 2 business
days
as the support professional working with you may need further
investigation to reach the most efficient resolution. The
offering
is
not appropriate for situations that require urgent, real-time
or
phone-based interactions or complex project analysis and dump
analysis issues. Issues of this nature are best handled working
with
a dedicated Microsoft Support Engineer by contacting Microsoft
Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and
confers
no
rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,081
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top