Using the ASP.Net membership provider in a Windows forms application part 2.
In my last blog
entry I showed how to use the ASP.Net membership provider in a Windows
application. In the sample I used the Membership.CreateUser() function to
create a new user. However I had to supply quite a bit of information, like
password question and answer, that I would not use in a typical windows
application. Fortunately the Membership.CreateUser() also has an overload with
just the username and password combination.
Dim user As MembershipUser
user = Membership.CreateUser("User2",
"Password")
Console.WriteLine(user.UserName)
However trying to use
this overload results in the error: “The password-answer supplied is
invalid.”. Additionally there might be some other properties you might
like to change, like the requirement for at least one non alphanumeric
character in the password. Even though these settings are exposed as properties
they are read-only so setting them like:
Membership.MinRequiredNonAlphanumericCharacters = 0
Will not work. To change them we need to use the app.config so lets add
one to the application. Add the following to the bottom of the <configuration>
section:
<system.web>
<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="false"
applicationName="/"
requiresUniqueEmail="false"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="1"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
passwordStrengthRegularExpression="" />
</providers>
</membership>
</system.web>
This removes the default membership provider as defined in the
machine.config and adds the same one but this time with more relaxed password
format and questions settings.
Now run the application again and this time we are allowed to add a
user with just a username and password combination. Because of the relaxed
password requirements even a password of just 1 single letter is accepted.
Stay posted for more.
Maurice de Beijer
www.TheProblemSolver.nl