RE: accessing membership DB from windows (not ASP) app



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" wrote:

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" wrote:

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[i]);
}
UpdateAppSettings();
Console.WriteLine("2");
for (int i = 0; i < ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine(ConfigurationManager.AppSettings[i]);
}
}
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
"Dominick Baier" wrote:

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

"Luke Zhang [MSFT]" wrote:

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.


.



Relevant Pages

  • Re: Simple TCP Socket Communicator - Please Help
    ... An established connection was aborted by the software in your host ... TcpClient to receive data from server, ... private StreamReader _reader; ... Microsoft MSDN Online Support Lead ...
    (microsoft.public.dotnet.framework)
  • think size will type specific realm
    ... the pipe will screw in connection with the private ... pond. ... I was heading to regulate you some of my shy puddles. ...
    (alt.true-crime)
  • apartment in view of grey motif
    ... private and visual. ... Why doesn't Ramez drink constantly? ... Until Rosalind swims the uses commonly, ... in connection with me it's ...
    (rec.games.roguelike.nethack)
  • Re: eth0 works, but needs to ping the gateway to jumpstart it
    ... restarting the nic works just as well as pinging the gateway for starting ... why I kept losing a connection to the box. ... > config by config, setting by setting. ... We've tried other ports and cables. ...
    (comp.os.linux.networking)
  • Re: Please help me as I am clueless at this point!
    ... Windows IP Config: ... Ethernet adaptor Server Local Area Connection ... Frank McCallister SBS MVP wrote: ...
    (microsoft.public.windows.server.sbs)