locked
Why is the default boilerplate asp.net Register.aspx.cs code force the UserName = email address? RRS feed

  • Question

  • User2142845853 posted

    Trying to create basic authentication in a WebForm .net 4.5 with Individual auth, C#

    The system now sort of works, except when you run the Register page, it only asks for EMAIL, PASSWORD, PASSWORD (verify). Why is the UserName stuck as the email? Im so used to MVC world, am just not getting why this wont work.  After an email registers, if I change the username in the table to 'fred' and try to login, it FAILS. 

    What is needed to add a real UserName entry? 

        public partial class Register : Page
        {
            protected void CreateUser_Click(object sender, EventArgs e)
            {
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
                var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
                IdentityResult result = manager.Create(user, Password.Text);
                if (result.Succeeded)
                {

    Wednesday, December 12, 2018 4:01 PM

Answers

All replies

  • User753101303 posted

    Hi,

    It's just a starting point. IMO this is just to provide a simple template that works. if you add this field, handle what to do if taken already by someone else and maybe change this at a later time, it seems it could be enough.

    Wednesday, December 12, 2018 4:33 PM
  • User475983607 posted

    Trying to create basic authentication in a WebForm .net 4.5 with Individual auth, C#

    The system now sort of works, except when you run the Register page, it only asks for EMAIL, PASSWORD, PASSWORD (verify). Why is the UserName stuck as the email? Im so used to MVC world, am just not getting why this wont work.  After an email registers, if I change the username in the table to 'fred' and try to login, it FAILS. 

    What is needed to add a real UserName entry? 

        public partial class Register : Page
        {
            protected void CreateUser_Click(object sender, EventArgs e)
            {
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
                var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
                IdentityResult result = manager.Create(user, Password.Text);
                if (result.Succeeded)
                {

    It's just a template and you can change the markup any way you like.

    If you do not want the email used as the username and email address then simply update the code.  This assumes you added a Username textbox to the registration form.

    var user = new ApplicationUser() { UserName = Username.Text, Email = Email.Text };

    Wednesday, December 12, 2018 4:36 PM
  • User2142845853 posted

    Yes its a template, but I modified just as you did, try to run?

    Unable to find control id 'Text' referenced by the 'ControlToValidate' property of ''.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Web.HttpException: Unable to find control id 'Text' referenced by the 'ControlToValidate' property of ''.
    

    But need to control the UserName, because im going to validate against a custom table.  shouldnt be so difficult. Its got to be the .aspx code

    rogersbr

    Trying to create basic authentication in a WebForm .net 4.5 with Individual auth, C#

    The system now sort of works, except when you run the Register page, it only asks for EMAIL, PASSWORD, PASSWORD (verify). Why is the UserName stuck as the email? Im so used to MVC world, am just not getting why this wont work.  After an email registers, if I change the username in the table to 'fred' and try to login, it FAILS. 

    What is needed to add a real UserName entry? 

        public partial class Register : Page
        {
            protected void CreateUser_Click(object sender, EventArgs e)
            {
                var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
                var user = new ApplicationUser() { UserName = Email.Text, Email = Email.Text };
                IdentityResult result = manager.Create(user, Password.Text);
                if (result.Succeeded)
                {

    It's just a template and you can change the markup any way you like.

    If you do not want the email used as the username and email address then simply update the code.  This assumes you added a Username textbox to the registration form.

    var user = new ApplicationUser() { UserName = Username.Text, Email = Email.Text };

    Wednesday, December 12, 2018 6:54 PM
  • User2142845853 posted

    Well Im getting closer.  I have the page where it will accept a username now, but the stored password login FAILS.  I register, password and all, confirm, it accepts it from the email, says confirmed now login?  click and the password is rejected or login rejected.  

    It didnt even write the newly registered account in the table. at all

    OMG, has some delay issue.  it shouldve worked, didnt, somehow had to refresh something like the table itself because after a delay it works. so the username is storing fine, it seems to login.

    but dont know why I put the changes, ran, didnt work.  end up putting back those same changes and then it does work. so for now the problem has to do with it refreshing something, or not refreshing and getting an error, then blink and it works.

    Wednesday, December 12, 2018 7:09 PM
  • User2142845853 posted

    So it looks like there is a timing issue.  Does there need to be Await style access to the table?? It gets the records back, the email is in there but it doesnt see it?

     protected void Forgot(object sender, EventArgs e)
            {
                if (IsValid)
                {
                    // Validate the user's email address
                    var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
                    ApplicationUser user = manager.FindByName(Email.Text);
                    if (user == null || !manager.IsEmailConfirmed(user.Id))
                    {
                        FailureText.Text = "The user either does not exist or is not confirmed.";
                        ErrorMessage.Visible = true;
                        return;
                    }
                    // F

    The Id in user.Id throws a null reference exception.

    And manager.FindByName(Email.Text) does not work, it returns null when you can see the results view showing the 2 currently registered users, the email.text matches but the code fails, saying NULL? then drops into the failuretext.text.

    Wednesday, December 12, 2018 7:25 PM
  • User2142845853 posted

    confirmed. the user manager fails to consistently see stored emails.  

    Put this in a try/catch, it cannot verify emails that are clearly there, so unless there is some cause this looks like a bug in the boilerplate code

    Wednesday, December 12, 2018 7:46 PM
  • User475983607 posted

    rogersbr

    confirmed. the user manager fails to consistently see stored emails.  

    Put this in a try/catch, it cannot verify emails that are clearly there, so unless there is some cause this looks like a bug in the boilerplate code

    The code below finds a user by username not email address.  Remember you specifically wanted a separate email and unsername which is how we got to this point.

    ApplicationUser user = manager.FindByName(Email.Text);

    https://docs.microsoft.com/en-us/previous-versions/aspnet/mt151639%28v%3dvs.108%29

    If you want to find a user by email address then use the appropriate UserManager method.

    manager.FindByEmail(Email.Text);

    https://docs.microsoft.com/en-us/previous-versions/aspnet/mt151641%28v%3dvs.108%29

    I strongly recommend that you read the reference documents so that you know what methods are available and how they work.  I provided the following link a previous post.

    https://docs.microsoft.com/en-us/previous-versions/aspnet/dn613290(v%3dvs.108)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 12, 2018 8:13 PM
  • User2142845853 posted

    You are right, thanks!  So I saw how it said FindByName, but then assumed it was using "Name" in a generalized way. Im studying the links, also select the method names or other names and right click/peek help.

    makes alot more sense now and it works

    rogersbr

    confirmed. the user manager fails to consistently see stored emails.  

    Put this in a try/catch, it cannot verify emails that are clearly there, so unless there is some cause this looks like a bug in the boilerplate code

    The code below finds a user by username not email address.  Remember you specifically wanted a separate email and unsername which is how we got to this point.

    ApplicationUser user = manager.FindByName(Email.Text);

    https://docs.microsoft.com/en-us/previous-versions/aspnet/mt151639%28v%3dvs.108%29

    If you want to find a user by email address then use the appropriate UserManager method.

    manager.FindByEmail(Email.Text);

    https://docs.microsoft.com/en-us/previous-versions/aspnet/mt151641%28v%3dvs.108%29

    I strongly recommend that you read the reference documents so that you know what methods are available and how they work.  I provided the following link a previous post.

    https://docs.microsoft.com/en-us/previous-versions/aspnet/dn613290(v%3dvs.108)

    Thursday, December 13, 2018 4:13 AM