I just downloaded Starter Site. And this time in the Register.cshtml page I get an error. When I try to create Account the error that is shown is: Provider encountered an unknown error.
I replaced this code:
WebSecurity.CreateAccount(emaill, pas..);
with this one:
WebSecurity.CreateUserAndAccount(email, pass..);
Then the account was created. Everything went fine. Why is it so?
Read more of my web development posts on my website. Thank you.
May be you might have not made the necessary changes to the below given method. Please check it. Mostly this is the reason why you got that provider error.
InitializeDatabaseConnection
"Helpful then please Mark as Answer"
http://www.abhishekluv.in
Github: https://github.com/abhishekluv
Its same. I get the basic: welcome, justin17862@gmail.com!
I just tried it and it is working completely fine.
Here is C# code:
@* Remove this section if you are using bundling *@
@section Scripts {
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
}
@{
Layout = "~/_SiteLayout.cshtml";
Page.Title = "Register";
// Initialize general page variables
var email = "";
var password = "";
var confirmPassword = "";
// Setup validation
Validation.RequireField("email", "You must specify an email address.");
Validation.RequireField("password", "Password cannot be blank.");
Validation.Add("confirmPassword",
Validator.EqualsTo("password", "Password and confirmation password do not match."));
Validation.Add("password",
Validator.StringLength(
maxLength: Int32.MaxValue,
minLength: 6,
errorMessage: "Password must be at least 6 characters"));
// If this is a POST request, validate and process data
if (IsPost) {
AntiForgery.Validate();
email = Request.Form["email"];
password = Request.Form["password"];
confirmPassword = Request.Form["confirmPassword"];
// Validate the user's captcha answer
// if (!ReCaptcha.Validate("PRIVATE_KEY")) {
// ModelState.AddError("recaptcha", "Captcha response was not correct");
// }
// If all information is valid, create a new account
if (Validation.IsValid()) {
// Insert a new user into the database
var db = Database.Open("StarterSite");
// Check if user already exists
var user = db.QuerySingle("SELECT Email FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", email);
if (user == null) {
// Insert email into the profile table
db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);
// Create and associate a new entry in the membership database.
// If successful, continue processing the request
try {
bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);
if (requireEmailConfirmation) {
var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
var confirmationUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/Account/Confirm?confirmationCode=" + HttpUtility.UrlEncode(token));
WebMail.Send(
to: email,
subject: "Please confirm your account",
body: "Your confirmation code is: " + token + ". Visit <a href=\"" + confirmationUrl + "\">" + confirmationUrl + "</a> to activate your account."
);
}
if (requireEmailConfirmation) {
// Thank the user for registering and let them know an email is on its way
Response.Redirect("~/Account/Thanks");
} else {
// Navigate back to the homepage and exit
WebSecurity.Login(email, password);
Response.Redirect("~/");
}
} catch (System.Web.Security.MembershipCreateUserException e) {
ModelState.AddFormError(e.Message);
}
} else {
// User already exists
ModelState.AddFormError("Email address is already in use.");
}
}
}
}
HTML
<hgroup class="title">
<h1>@Page.Title.</h1>
<h2>Create a new account.</h2>
</hgroup>
<form method="post">
@AntiForgery.GetHtml()
@* If at least one validation error exists, notify the user *@
@Html.ValidationSummary("Account creation was unsuccessful. Please correct the errors and try again.", excludeFieldErrors: true, htmlAttributes: null)
<fieldset>
<legend>Registration Form</legend>
<ol>
<li class="email">
<label for="email" @if(!ModelState.IsValidField("email")){<text>class="error-label"</text>}>Email address</label>
<input type="text" id="email" name="email" value="@email" @Validation.For("email") />
@* Write any email validation errors to the page *@
@Html.ValidationMessage("email")
</li>
<li class="password">
<label for="password" @if(!ModelState.IsValidField("password")) {<text>class="error-label"</text>}>Password</label>
<input type="password" id="password" name="password" @Validation.For("password") />
@* Write any password validation errors to the page *@
@Html.ValidationMessage("password")
</li>
<li class="confirm-password">
<label for="confirmPassword" @if(!ModelState.IsValidField("confirmPassword")) {<text>class="error-label"</text>}>Confirm password</label>
<input type="password" id="confirmPassword" name="confirmPassword" @Validation.For("confirmPassword") />
@* Write any password validation errors to the page *@
@Html.ValidationMessage("confirmPassword")
</li>
<li class="recaptcha">
<div class="message-info">
<p>
To enable CAPTCHA verification, <a href="http://go.microsoft.com/fwlink/?LinkId=204140">install the
ASP.NET Web Helpers Library</a> and uncomment ReCaptcha.GetHtml and replace 'PUBLIC_KEY'
with your public key. At the top of this page, uncomment ReCaptcha. Validate and
replace 'PRIVATE_KEY' with your private key.
Register for reCAPTCHA keys at <a href="http://recaptcha.net">reCAPTCHA.net</a>.
</p>
</div>
@*
@ReCaptcha.GetHtml("PUBLIC_KEY", theme: "white")
@Html.ValidationMessage("recaptcha")
*@
</li>
</ol>
<input type="submit" value="Register" />
</fieldset>
</form>
I have not changed any of the _AppStart settings or anything just downloaded the template and started it and it worked fine. I think the issue might be the extra column. How are you storing the persons Name?
CreateAccount only accepts 3 arguments. (Username, Password, ConfirmationToken) last ones optional. If you want to add aditional info use CreateUserAndAccount method.
db.Execute("INSERT INTO UserProfile (Email, Name) VALUES (@0, @1)", email, name);
// Create and associate a new entry in the membership database.
// If successful, continue processing the request
try {
bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);
Is there any fault?
Read more of my web development posts on my website. Thank you.
Yes, your inserting the email and name then doing it again (inserting email) through WebSecurity.CreateAccount, that's causing an error. Your basically trying to insert the email into same row twice. Use the WebSecurity.CreateuserAndAccount method it will
save you so much time..
Yes, your inserting the email and name then doing it again (inserting email) through WebSecurity.CreateAccount, that's causing an error. Your basically trying to insert the email into same row twice. Use the WebSecurity.CreateuserAndAccount method it will
save you so much time..
Why and how? What's the reason behind that?
Read more of my web development posts on my website. Thank you.
Contributor
4010 Points
1926 Posts
WebSecurity
Aug 18, 2013 11:01 AM|Afzaal.Ahmad.Zeeshan|LINK
Hi,
I just downloaded Starter Site. And this time in the Register.cshtml page I get an error. When I try to create Account the error that is shown is: Provider encountered an unknown error.
I replaced this code:
with this one:
Then the account was created. Everything went fine. Why is it so?
Participant
1447 Points
641 Posts
Re: WebSecurity
Aug 18, 2013 11:12 AM|Abhishek Luv|LINK
I just tried. There were no errors.
http://www.abhishekluv.in
Github: https://github.com/abhishekluv
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 11:13 AM|Afzaal.Ahmad.Zeeshan|LINK
Well I did get an error. I will try it again!
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 11:16 AM|Afzaal.Ahmad.Zeeshan|LINK
You can have a look.
Participant
1447 Points
641 Posts
Re: WebSecurity
Aug 18, 2013 11:18 AM|Abhishek Luv|LINK
Please post your Register code here. Lets see.
http://www.abhishekluv.in
Github: https://github.com/abhishekluv
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 11:22 AM|Afzaal.Ahmad.Zeeshan|LINK
I deleted!
Participant
1447 Points
641 Posts
Re: WebSecurity
Aug 18, 2013 11:30 AM|Abhishek Luv|LINK
May be you might have not made the necessary changes to the below given method. Please check it. Mostly this is the reason why you got that provider error.
InitializeDatabaseConnection
http://www.abhishekluv.in
Github: https://github.com/abhishekluv
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 11:31 AM|Afzaal.Ahmad.Zeeshan|LINK
No no thats OK! Correct Email, correct userid.
Participant
1447 Points
641 Posts
Re: WebSecurity
Aug 18, 2013 11:57 AM|Abhishek Luv|LINK
N what about your username?
http://www.abhishekluv.in
Github: https://github.com/abhishekluv
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 12:03 PM|Afzaal.Ahmad.Zeeshan|LINK
Actually the string is as this:
I was able to create the Account using the second method. But using CreateAccount had some issues.
Participant
1447 Points
641 Posts
Re: WebSecurity
Aug 18, 2013 12:11 PM|Abhishek Luv|LINK
Show your UserProfile table. Is it the defualt UserProfile table or you have made some changes to it.
http://www.abhishekluv.in
Github: https://github.com/abhishekluv
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 12:14 PM|Afzaal.Ahmad.Zeeshan|LINK
No the default one!
Participant
1447 Points
641 Posts
Re: WebSecurity
Aug 18, 2013 12:16 PM|Abhishek Luv|LINK
Ok. But the image which you have posted in this post has a Your Name textbox.
http://www.abhishekluv.in
Github: https://github.com/abhishekluv
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 12:18 PM|Afzaal.Ahmad.Zeeshan|LINK
Yes, I added a new column name: Name. But that won't cause any trouble
Participant
1447 Points
641 Posts
Re: WebSecurity
Aug 18, 2013 12:38 PM|Abhishek Luv|LINK
For the Login Welcome message are you using the user Email or the Username of the user.
http://www.abhishekluv.in
Github: https://github.com/abhishekluv
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 12:39 PM|Afzaal.Ahmad.Zeeshan|LINK
Its same. I get the basic: welcome, justin17862@gmail.com!
Member
414 Points
518 Posts
Re: WebSecurity
Aug 18, 2013 01:10 PM|CriticalError|LINK
I just tried it and it is working completely fine.
Here is C# code:
HTML
I have not changed any of the _AppStart settings or anything just downloaded the template and started it and it worked fine. I think the issue might be the extra column. How are you storing the persons Name?
CreateAccount only accepts 3 arguments. (Username, Password, ConfirmationToken) last ones optional. If you want to add aditional info use CreateUserAndAccount method.
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 01:19 PM|Afzaal.Ahmad.Zeeshan|LINK
@CriticalError, No actually I am just using INSERT INTO to save the name. I am not saving the Name anywhere else!
Member
414 Points
518 Posts
Re: WebSecurity
Aug 18, 2013 01:44 PM|CriticalError|LINK
Just you use CreateUserAndAccount I don't see why you would insert data into the same row using different methods.
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 01:49 PM|Afzaal.Ahmad.Zeeshan|LINK
I was using this:
Is there any fault?
Member
414 Points
518 Posts
Re: WebSecurity
Aug 18, 2013 01:56 PM|CriticalError|LINK
Yes, your inserting the email and name then doing it again (inserting email) through WebSecurity.CreateAccount, that's causing an error. Your basically trying to insert the email into same row twice. Use the WebSecurity.CreateuserAndAccount method it will save you so much time..
Contributor
4010 Points
1926 Posts
Re: WebSecurity
Aug 18, 2013 02:06 PM|Afzaal.Ahmad.Zeeshan|LINK
Why and how? What's the reason behind that?
Member
414 Points
518 Posts
Re: WebSecurity
Aug 18, 2013 02:40 PM|CriticalError|LINK
Because that's the proper way to do it as the method is providded, plus you can easily add columns / data easily.
http://www.thecodingguys.net/reference/asp/websecurity-createuserandaccount