我们正在使用ASP.Net成员资格提供程序(SQL Server提供程序),我希望能够在安装程序中设置Admin用户帐户。为此,我需要配置ASP.Net成员资格提供程序,以便我的安装程序可以使用它 - 但我不想为安装程序设置配置文件。
那么有没有一种方法可以通过代码配置ASP.Net成员资格而无需编写自定义提供程序?
答案 0 :(得分:7)
好的,按照他们在这里提出的解决方案:http://forums.asp.net/p/997608/2209437.aspx
我创建了一个类,在无参数构造函数中(您需要)只从命令行参数获取服务器和端口。这样我就可以去“MembershipInitializer.exe”SomeSqlServer \ Instance“51000.
public class CustomSQLMembershipProvider : SqlMembershipProvider {
private readonly string _server;
private readonly string _port;
/// <summary>
/// Initializes a new instance of the <see cref="T:System.Web.Security.SqlMembershipProvider"/> class.
/// </summary>
public CustomSQLMembershipProvider() {
string[] args = System.Environment.GetCommandLineArgs();
// args[0] is the exe name
_server = args[1];
_port = args[2];
}
public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
{
base.Initialize(name, config);
// Update the private connection string field in the base class.
string connectionString = string.Format(@"Data Source={0},{1};Initial Catalog=aspnetdb;UID=NICCAMembership;PWD=_Password1;Application Name=NICCA;Connect Timeout=120;", _server, _port);
// Set private property of Membership provider.
FieldInfo connectionStringField = GetType().BaseType.GetField("_sqlConnectionString", BindingFlags.Instance | BindingFlags.NonPublic);
connectionStringField.SetValue(this, connectionString);
}
}
在您的控制台应用(或Windows应用)的app.config中
<configuration>
<connectionStrings>
<clear/>
<add name="MembershipDB"
connectionString="Some Bogus String here that gets overrided in the custom class"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<authentication mode="Forms"/>
<authorization>
<deny users="?"/>
</authorization>
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider"/>
<add name="AspNetSqlMembershipProvider"
connectionStringName="MembershipDB"
applicationName="NICCA"
type="MyInitializeMembershipDB.CustomSQLMembershipProvider, MyInitializeMembershipDB"
requiresUniqueEmail="false"
requiresQuestionAndAnswer="false"/>
</providers>
</membership>
</system.web>
</configuration>
如果您还需要角色提供程序位,则可能必须以相同的方式覆盖SqlRoleProvider。
如果您正在创建一个程序来自动将您的成员资格数据库初始化为特定的状态,但是在有人将其输入安装向导之前,不知道sql server地址和端口名称,这样做就可以了。
这有帮助吗?
答案 1 :(得分:-1)
Membership.ApplicationName = "yourfbaApplicationame";
MembershipUser user = Membership.CreateUser("admin,"password","emaiol@g.com");
并确保配置文件中包含以下条目。
<connectionStrings>
<add connectionString="server=sqd01-1-cll;database=FBA;Integrated Security=SSPI;" name="FBASqlConnString" providerName="System.Data.SqlClient"/>
如果您仍想使用完整代码而不需要使用配置文件。使用以下
SqlMembershipProvider ObjSqlMembershipProvider = new SqlMembershipProvider();
SqlRoleProvider ObjSqlRoleProvider = new SqlRoleProvider();
NameValueCollection ObjNameValueCollRole = new NameValueCollection();
NameValueCollection ObjNameValueCollMembership = new NameValueCollection();
MembershipCreateStatus enMembershipCreateStatus;
ObjNameValueCollMembership.Add("connectionStringName", "Connection String Name");
ObjNameValueCollMembership.Add("applicationName", "ApplicatioNAme");
//these items are assumed to be Default and dont care..Should be given a look later stage.
ObjNameValueCollMembership.Add("enablePasswordRetrieval", "false");
ObjNameValueCollMembership.Add("enablePasswordReset", "false");
ObjNameValueCollMembership.Add("requiresQuestionAndAnswer", "false");
ObjNameValueCollMembership.Add("requiresUniqueEmail", "false");
ObjNameValueCollMembership.Add("passwordFormat", "Hashed");
ObjNameValueCollMembership.Add("maxInvalidPasswordAttempts", "5");
ObjNameValueCollMembership.Add("minRequiredPasswordLength", "1");
ObjNameValueCollMembership.Add("minRequiredNonalphanumericCharacters", "0");
ObjNameValueCollMembership.Add("passwordAttemptWindow", "10");
ObjNameValueCollMembership.Add("passwordStrengthRegularExpression", "");
//hard coded the Provider Name,This function just need one that is present. I tried other names and it throws error. I found this using Reflector ..all the rest are take care by the above
//name value pairs
ObjSqlMembershipProvider.Initialize("AspNetSqlMembershipProvider", ObjNameValueCollMembership);MembershipUser user = ObjSqlMembershipProvider.CreateUser("admin,"password","emaiol@g.com");
一个这个你需要在Config文件中给出连接字符串,而不是其他的。如果您希望从代码中继承类