How to change default membership provider value in asp.net - asp.net

I have declare multiple membership provider in web.config
<membership userIsOnlineTimeWindow="15" defaultProvider="UmbracoMembershipProvider">
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
<add name="UmbracoMembershipProvider" type="umbraco.providers.members.UmbracoMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" defaultMemberTypeAlias="Another Type" passwordFormat="Hashed" />
<add name="Reset_Pass" connectionStringName="AspNetDB" applicationName="/" enablePasswordReset="true" enablePasswordRetrieval="false" maxInvalidPasswordAttempts="100000" minRequiredPasswordLength="5" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="100000" passwordStrengthRegularExpression="" passwordFormat="Hashed" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" type="SiteMemberShipProvider" />
</providers>
</membership>
But when i call following , Membership is assign with value of default provider.
public static MembershipUser GetUserInfoByUserName(string username)
{
MembershipUser user = Membership.GetUser(username);
return user;
}
when I monitor value on Membership value it set value of defaultProvider
How can I change membership provider value to "AspNetSqlMembershipProvider".

Related

Could not load file or assembly 'MySql.Web, Version=6.8.3.0

ASP.NET configuration throws Could not load file or assembly 'MySql.Web, Version=6.8.3.0,0,… when trying to add a new user
<add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false"
enablePasswordReset="true" requiresQuestionAndAnswer="true"
applicationName="/" requiresUniqueEmail="false" passwordFormat="Clear"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10"
passwordStrengthRegularExpression="" />
You might install/reinstall MySQL Connector or try the steps below:
1) Go to machine.Config.
for 32 bit Operating System:
C:\Windows\Microsoft.NET\Framework[version]\config\machine.config
for 64 bit Operating System:
C:\Windows\Microsoft.NET\Framework64[version]\config\machine.config
2) Then remove or comment out these lines below:
<add name="MySQLRoleProvider" type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" applicationName="/"/>
<add name="MySQLMembershipProvider" type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, Version=6.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" connectionStringName="LocalMySqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" applicationName="/" requiresUniqueEmail="false" passwordFormat="Clear" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>

how use Custom Membership in website and set in web.config

I have Custom Membership Provider in asp.net 4. it's:
public class CustomMembershipProvider : SqlMembershipProvider
{
}
and Custom Role Provider:
public class CustomRoleProvider : RoleProvider
{
}
I use this code for set Custom Membership in web.config:
<membership defaultProvider="CustomMembershipProvider">
<providers>
<clear/>
<add name="CustomMembershipProvider" type="Login1.Code.CustomMembershipProvider" connectionStringName="LoginDB1Entities" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="CustomRoleProvider">
<providers>
<clear/>
<add name="CustomRoleProvider" type="Login1.Code.CustomRoleProvider" connectionStringName="LoginDB1Entities" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
and this connection string:
<connectionStrings>
<add name="LoginDB1Entities" connectionString="metadata=res://*/Code.EFModel.csdl|res://*/Code.EFModel.ssdl|res://*/Code.EFModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.;Initial Catalog=LoginDB1;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
my application its working in localhost but i use this site in host and get this error:
Server Error in '/' Application.

Integration tests: MembershipProvider - TypeLoadException

Am trying to write some tests that create/remove/change users using a CustomMembershipProvider. The CustomMembershipProvider is part of the WebApp assembly.
When I run my test I get the following exception:
System.TypeLoadException : Could not load type 'WebApp.Framework.CustomMembershipProvider' from assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type)
at System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, ref StackCrawlMark stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName)
at System.Type.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase)
at System.Web.Security.Membership.InitializeSettings(Boolean initializeGeneralSettings, RuntimeConfig appConfig, MembershipSection settings)
at System.Web.Security.Membership.Initialize()
at System.Web.Security.Membership.get_Providers()
at System.Web.Security.MembershipUser..ctor(String providerName, String name, Object providerUserKey, String email, String passwordQuestion, String comment, Boolean isApproved, Boolean isLockedOut, DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate, DateTime lastPasswordChangedDate, DateTime lastLockoutDate)
at BusinessLayer.Users.UserRepository.Add(User user, ref MembershipCreateStatus status) in UserRepository.cs: line 43
at WebApp.Framework.CustomMembershipProvider.CreateUser(String username, String password, String email, String passwordQuestion, String passwordAnswer, Boolean isApproved, Object providerUserKey, ref MembershipCreateStatus status) in CustomMembershipProvider.cs: line 86
at Tests.Framework.CustomMembershipProviderTests.CreateUser() in CustomMembershipProviderTests.cs: line 22
The exception is thrown when I execute this line of code:
return new MembershipUser(
"MyMembershipProvider",
user.Name,
null,
user.EmailAddress,
string.Empty,
string.Empty,
true,
false,
DateTime.MinValue,
DateTime.MinValue,
DateTime.MinValue,
DateTime.MinValue,
DateTime.MinValue);
In my test project the following lines are added to my app.conf:
the membership definition:
<system.web>
<membership defaultProvider="MyMembershipProvider">
<providers>
<clear/>
<add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider" connectionStringName="DefaultConnection"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
</system.web>
the database connection:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=WS050129\SQLEXPRESS;Initial Catalog=Users;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>
It looks good to me, but apparently something is off :). Could anybody point me in the right direction? Kinda clueless here.
Many thanks.
Try changing this:
<add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider" connectionStringName="DefaultConnection"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
... to this:
<add name="MyMembershipProvider" type="WebApp.Framework.CustomMembershipProvider, WebApp" connectionStringName="DefaultConnection"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
You may need to tweak that a little. But it looks like the problem is that, lacking information about what assembly the type is in, the framework is looking in System.Web. Flesh out the type definition attribute on that tag.

Custom RoleProvider Not Called

My application has custom Role and MembershipProviders. I've registered them in web.config, but when I try to do if(User.IsInRole("Blah")), neither of my breakpoints in the RoleProvider's Initialize or IsUserInRole are hit. The membership provider works fine, so I guess there must be something I've missed from web.config. This is what I have:
<system.web>
...
<membership defaultProvider="MyAppMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add name="MyAppMembershipProvider"
type="MyAppMembership.MyAppMembershipProvider"
connectionStringName="MyApp"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" writeExceptionsToEventLog="false" />
</providers>
</membership>
<roleManager defaultProvider="MyAppRoleProvider">
<providers>
<clear />
<add name="MyAppRoleProvider"
type="MyAppMembership.MyAppRoleProvider"
connectionStringName="MyApp"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" writeExceptionsToEventLog="false" />
</providers>
</roleManager>
</system.web>
Is there something else which I need?
The attribute enabled of the the <roleManager>-Element defaults to false! Try:
<roleManager enabled="true" defaultProvider="MyAppRoleProvider">
<providers>
<clear />
<add name="MyAppRoleProvider"
type="MyAppMembership.MyAppRoleProvider"
connectionStringName="MyApp"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" writeExceptionsToEventLog="false" />
</providers>
</roleManager>

Changing the Password format in Membership

How do I change the required password format in membership?
From http://msdn.microsoft.com/en-us/library/ff648345.aspx :
In your web.config :
<system.web>
...
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add
name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="MySqlConnection"
applicationName="MyApplication"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
requiresUniqueEmail="true"
passwordFormat="Hashed" />
</providers>
</membership>
Modify passwordFormat to what you'd like :
The SQL Server membership provider
supports Clear, Encrypted, and Hashed
password formats.
I think that you need parameters like these in your config: minRequiredPasswordLength, minRequiredNonalphanumericCharacters and passwordStrengthRegularExpression.
From http://weblogs.asp.net/owscott/archive/2005/05/11/Changing-the-Password-Complexity-in-ASP.NET-V2.0.aspx:
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider" />
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="LocalSqlServer"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
applicationName="/"
requiresUniqueEmail="false"
minRequiredPasswordLength="1"
minRequiredNonalphanumericCharacters="0"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
passwordAttemptWindow="10"
passwordStrengthRegularExpression="" />
</providers>
</membership>

Resources