locked
Identity Login with mobile phone number RRS feed

  • Question

  • User-516026232 posted

    I am using Identity in .NET to make my token based authentication. At the moment the login is done using the username (email) and password, but I would also like to add the possibility to login using other information like for instance mobile phone number.

    What is the best way to achieve it? My idea is to implement a Login Endpoint that will search the database to get the users email and makes a call to the current Login endpoint /Token just like the way it is (following this idea), but I wonder if there is no other option available to make this without the necessity to make a double call. Any suggestion would be welcome.

    Monday, May 15, 2017 5:31 PM

Answers

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 15, 2017 5:58 PM
  • User-707554951 posted

    Hi  qsoft_developer,

    Are you want to use mobile phone number and password to do login.

    If that the case.

    You just need to change register.aspx page like this:

     <div>
            <h4 style="font-size: medium">Register a new user</h4>
            <hr />
            <p>
                <asp:Literal runat="server" ID="StatusMessage" />
            </p>                
            <div style="margin-bottom:10px">
                <asp:Label runat="server" AssociatedControlID="Phonenumber">Phone number</asp:Label>
                <div>
                    <asp:TextBox runat="server" ID="Phonenumber" />                
                </div>
    
            </div>
            <div style="margin-bottom:10px">
                <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                <div>
                    <asp:TextBox runat="server" ID="Password" TextMode="Password" />                
                </div>
            </div>
            <div style="margin-bottom:10px">
                <asp:Label runat="server" AssociatedControlID="ConfirmPassword">Confirm password</asp:Label>
                <div>
                    <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />                
                </div>
            </div>
            <div>
                <div>
                    <asp:Button runat="server" ID="CreateUser"  OnClick="CreateUser_Click" Text="Register" />
                </div>
            </div>
        </div>
    

    Codebehind:

    protected void CreateUser_Click(object sender, EventArgs e)
            {
                // Default UserStore constructor uses the default connection string named: DefaultConnection
                var userStore = new UserStore<IdentityUser>();
                var userManager = new UserManager<IdentityUser>(userStore);
                var user = new IdentityUser() {  UserName = Phonenumber.Text,};
                IdentityResult identityResult = userManager.Create(user, Password.Text);
                if (identityResult.Succeeded) {
                    var authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
                    var userIdentity = userManager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
                    authenticationManager.SignIn(new AuthenticationProperties() { }, userIdentity);
                    Response.Redirect("~/Account/Login.aspx");
                 //   StatusMessage.Text = String.Format("User {0} was created successfully", Phonenumber.Text);
                }
                else
                {
                    StatusMessage.Text = identityResult.Errors.FirstOrDefault();
                }
            }

    After register, you could use phone number and password to login in.

    Best regards

    Cathy

    Tuesday, May 16, 2017 7:48 AM
  • User-516026232 posted

    But I want to have both email and mobile phone authentication at the same time. 

    Tuesday, May 16, 2017 10:06 AM