Open Closed

Show registration page in custom login page #2949


0
safi created

Hi

I have a customized login page and I want to prevent registration page redirection like want to show the registration page on the login page so what will happen If I click on the registration button so it should hide the login fields and show registration form fields before redirecting to the registration page.

  • ABP Framework version: v4.4.3
  • UI type: Blazor
  • DB provider: EF Core
  • Tiered (MVC) or Identity Server Separated (Angular): no
  • Exception message and stack trace:
  • Steps to reproduce the issue:"

Thanks,


41 Answer(s)
  • 0
    liangshiwei created
    Support Team

    Hi,

    Of course, you can.

    You can custom RegisterModel to redirect to login page.

    [Dependency(ReplaceServices = true)]
    [ExposeServices(typeof(RegisterModel))]
    public class MyRegisterModel : RegisterModel
    {
        public override Task<IActionResult> OnGetAsync()
        {
            return Task.FromResult((IActionResult)Redirect("/account/login?Type=register"));
        }
    }
    

    On the login page, show register section if action parameter value is register,

    .....
    publc class MyLoginPage : ..
    {
        [BindProperty]
        public string Type{get;set;}
    }
    
    @if (Model.Type == "register")
    {
          <div>....</div>
    }else{
          <div>....</div>
    }
    
  • 0
    safi created

    @if (Model.Type == "register") { <div>....</div> }else{ <div>....</div> }

    Thanks for the reply do I need to add registration.cshtml page code to the custom login page if yes then on the same page can't add two models right?

    I have account pro code so what needs to do to show the registration form. Please let me know.

    Thanks,

  • 0
    safi created

    @if (Model.Type == "register")
    {
    <div>....</div>
    }else{
    <div>....</div>
    }

    Thanks for the reply do I need to add registration.cshtml page code to the custom login page if yes then on the same page can't add two models right?

    I have account pro code so what needs to do to show the registration form. Please let me know.

    Thanks,

    This is my login.cshtml page and myregistermodel

    Login.cshtml.cs page code but the problem is if I click on registration button I am not getting value in Type variable

  • 0
    liangshiwei created
    Support Team

    Hi,

    Try

    [BindProperty(SupportsGet = true)]
    public string Type {get; set;}
    
  • 0
    safi created

    [BindProperty(SupportsGet = true)] public string Type {get; set;}

    how can I add a registration form to the login page? I added the if-else condition but don't know-how will add the registration form code.

  • 0
    liangshiwei created
    Support Team

    Hi,

    I think you have the source code, you just need to copy it to the login page

  • 0
    safi created

    Hi,

    I think you have the source code, you just need to copy it to the login page

    yes I have and If I am pasting this code into login page then getting this error

  • 0
    safi created

    Hi,

    I think you have the source code, you just need to copy it to the login page

    yes I have and If I am pasting this code into login page then getting this error

    I added the registration form code but was not able to register the user.

  • 0
    safi created

    Hi

    On login button click getting below error

  • 0
    liangshiwei created
    Support Team

    Try:

    Since you want to implement registration and login functionality on one page, you know, some parameters are required for registering but not login, so you must manually validate the parameters.

    Remove ValidateModel(); and manually validate the parameters

  • 0
    safi created

    Try:

    Since you want to implement registration and login functionality on one page, you know, some parameters are required for registering but not login, so you must manually validate the parameters.

    Remove ValidateModel(); and manually validate the parameters

    Yes I removed and login functionality is working fine but registration is not working I am using the below code

    Can you please help me so that I can close this.

    Thanks,

  • 0
    safi created

    Try:

    Since you want to implement registration and login functionality on one page, you know, some parameters are required for registering but not login, so you must manually validate the parameters.

    Remove ValidateModel(); and manually validate the parameters

    Yes I removed and login functionality is working fine but registration is not working I am using the below code

    Can you please help me so that I can close this.

    Thanks,

    Please reply!

  • 0
    liangshiwei created
    Support Team

    Hi,

    Can you share the source code with me? [email protected]

  • 0
    safi created

    [email protected]

    Hi

    I can't share the full project so I shared working files with you in the mail can you please check and revert.

    Thanks,

  • 0
    liangshiwei created
    Support Team

    Try:

    <form method="post" action="@Url.Page("./Register", new { returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash })">

    Replace with

    <form method="post" asp-page="/account/register">

  • 0
    safi created

    Try:

    <form method="post" action="@Url.Page("./Register", new { returnUrl = Model.ReturnUrl, returnUrlHash = Model.ReturnUrlHash })">

    Replace with

    <form method="post" asp-page="/account/register">

    Yes thanks it's working but after registration it's going to swagger link.

  • 0
    liangshiwei created
    Support Team

    <form method="post" asp-page="/account/[email protected]">

  • 0
    safi created

    asp-page="/account/[email protected]"

    no it's not working.

  • 0
    liangshiwei created
    Support Team

    Hi,

    Sorry, I didn't get it, I think since you have the source code, you can do anything you want. of course, you can also change the redirect code.

    return Redirect("the URL you want to redirect ");

  • 0
    safi created

    Hi,

    Sorry, I didn't get it, I think since you have the source code, you can do anything you want. of course, you can also change the redirect code.

    return Redirect("the URL you want to redirect ");

    I customized the code to show registration on the login screen but don't have the full code in the project. I have only below code

  • 0
    liangshiwei created
    Support Team

    Hi,

    ....
    public class MyRegisterModel : RegisterModel
    {
    
        ......
    
        public async override Task<IActionResult> OnPostAsync()
        {
            try
            {
                await CheckSelfRegistrationAsync();
                await SetUseCaptchaAsync();
    
                IdentityUser user;
                if (IsExternalLogin)
                {
                    var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync();
                    if (externalLoginInfo == null)
                    {
                        Logger.LogWarning("External login info is not available");
                        return RedirectToPage("./Login");
                    }
    
                    user = await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress);
                }
                else
                {
                    var localLoginResult = await CheckLocalLoginAsync();
                    if (localLoginResult != null)
                    {
                        LocalLoginDisabled = true;
                        return localLoginResult;
                    }
    
                    user = await RegisterLocalUserAsync();
                }
    
                if (await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedEmail) && !user.EmailConfirmed ||
                    await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedPhoneNumber) && !user.PhoneNumberConfirmed)
                {
                    await StoreConfirmUser(user);
    
                    return RedirectToPage("./ConfirmUser", new {
                        returnUrl = ReturnUrl,
                        returnUrlHash = ReturnUrlHash
                    });
                }
    
                await SignInManager.SignInAsync(user, isPersistent: true);
    
                // this line
                return Redirect("the URL what you want to redirect");
            }
            catch (BusinessException e)
            {
                Alerts.Danger(GetLocalizeExceptionMessage(e));
                return Page();
            }
        }
    }
    
  • 0
    safi created

    Hi,

    .... 
    public class MyRegisterModel : RegisterModel 
    { 
     
        ...... 
     
        public async override Task<IActionResult> OnPostAsync() 
        { 
            try 
            { 
                await CheckSelfRegistrationAsync(); 
                await SetUseCaptchaAsync(); 
     
                IdentityUser user; 
                if (IsExternalLogin) 
                { 
                    var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync(); 
                    if (externalLoginInfo == null) 
                    { 
                        Logger.LogWarning("External login info is not available"); 
                        return RedirectToPage("./Login"); 
                    } 
     
                    user = await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress); 
                } 
                else 
                { 
                    var localLoginResult = await CheckLocalLoginAsync(); 
                    if (localLoginResult != null) 
                    { 
                        LocalLoginDisabled = true; 
                        return localLoginResult; 
                    } 
     
                    user = await RegisterLocalUserAsync(); 
                } 
     
                if (await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedEmail) && !user.EmailConfirmed || 
                    await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedPhoneNumber) && !user.PhoneNumberConfirmed) 
                { 
                    await StoreConfirmUser(user); 
     
                    return RedirectToPage("./ConfirmUser", new { 
                        returnUrl = ReturnUrl, 
                        returnUrlHash = ReturnUrlHash 
                    }); 
                } 
     
                await SignInManager.SignInAsync(user, isPersistent: true); 
     
                // this line 
                return Redirect("the URL what you want to redirect"); 
            } 
            catch (BusinessException e) 
            { 
                Alerts.Danger(GetLocalizeExceptionMessage(e)); 
                return Page(); 
            } 
        } 
    } 
    

    This method is not exist CheckLocalLoginAsync

  • 0
    liangshiwei created
    Support Team

    Hi,

    Sorry, I thought you were using the latest version.

    For version 4.4.3

    public class MyRegisterModel : RegisterModel
    {
    
        ......
        [UnitOfWork]
        public async override Task<IActionResult> OnPostAsync()
        {
            try
                {
                    await CheckSelfRegistrationAsync();
                    await SetUseCaptchaAsync();
    
                    IdentityUser user;
                    if (IsExternalLogin)
                    {
                        var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync();
                        if (externalLoginInfo == null)
                        {
                            Logger.LogWarning("External login info is not available");
                            return RedirectToPage("./Login");
                        }
    
                        user = await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress);
                    }
                    else
                    {
                        user = await RegisterLocalUserAsync();
                    }
    
                    if (await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedEmail) && !user.EmailConfirmed ||
                        await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedPhoneNumber) && !user.PhoneNumberConfirmed)
                    {
                        await StoreConfirmUser(user);
    
                        return RedirectToPage("./ConfirmUser", new
                        {
                            returnUrl = ReturnUrl,
                            returnUrlHash = ReturnUrlHash
                        });
                    }
    
                    await SignInManager.SignInAsync(user, isPersistent: true);
    
                    // this line 
                    return Redirect("the URL what you want to redirect");
                }
                catch (BusinessException e)
                {
                    Alerts.Danger(GetLocalizeExceptionMessage(e));
                    return Page();
                }
        }
    }
    
  • 0
    safi created

    Email is not working now also after registration it's going to swagger link.

  • 0
    safi created

    Hi,

    Sorry, I thought you were using the latest version.

    For version 4.4.3

    public class MyRegisterModel : RegisterModel 
    { 
     
        ...... 
        [UnitOfWork] 
        public async override Task<IActionResult> OnPostAsync() 
        { 
            try 
                { 
                    await CheckSelfRegistrationAsync(); 
                    await SetUseCaptchaAsync(); 
     
                    IdentityUser user; 
                    if (IsExternalLogin) 
                    { 
                        var externalLoginInfo = await SignInManager.GetExternalLoginInfoAsync(); 
                        if (externalLoginInfo == null) 
                        { 
                            Logger.LogWarning("External login info is not available"); 
                            return RedirectToPage("./Login"); 
                        } 
     
                        user = await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress); 
                    } 
                    else 
                    { 
                        user = await RegisterLocalUserAsync(); 
                    } 
     
                    if (await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedEmail) && !user.EmailConfirmed || 
                        await SettingProvider.IsTrueAsync(IdentitySettingNames.SignIn.RequireConfirmedPhoneNumber) && !user.PhoneNumberConfirmed) 
                    { 
                        await StoreConfirmUser(user); 
     
                        return RedirectToPage("./ConfirmUser", new 
                        { 
                            returnUrl = ReturnUrl, 
                            returnUrlHash = ReturnUrlHash 
                        }); 
                    } 
     
                    await SignInManager.SignInAsync(user, isPersistent: true); 
     
                    // this line  
                    return Redirect("the URL what you want to redirect"); 
                } 
                catch (BusinessException e) 
                { 
                    Alerts.Danger(GetLocalizeExceptionMessage(e)); 
                    return Page(); 
                } 
        } 
    } 
    

    I have added debugger in this but not working debugger here directly going in swagger link

  • 0
    liangshiwei created
    Support Team

    Can you share your code?

  • 0
    safi created

    Can you share your code?

    Hi

    Shared code files in mail also shared appsettings as well.

  • 0
    safi created

    Can you share your code?

    Hi

    Shared code files in mail also shared appsettings as well.

    Are you checking my code?

  • 0
    liangshiwei created
    Support Team

    you needed change it, replace <the URL what you want to redirect> with the URL you want.

    // this line 
    return Redirect("<the URL what you want to redirect>");
    
  • 0
    safi created

    return Redirect("<the URL what you want to redirect>");

    Yes I changed but still not able to debug this method also it's going to swagger link

  • 0
    safi created

    you needed change it, replace <the URL what you want to redirect> with the URL you want.

    // this line  
    return Redirect("<the URL what you want to redirect>"); 
    

    Also user is not inserting into db table so please help

  • 0
    liangshiwei created
    Support Team

    https://zoom.us/j/95048339717?pwd=cWcxVGhpREZYT2RKdGl0YjcwT2hGZz09

  • 0
    liangshiwei created
    Support Team

    Solved

  • 0
    safi created

    Solved

    Now, forgot password is not working.

  • 0
    liangshiwei created
    Support Team

    I think the problem is resolved, you can check the code to do the same thing.

    Remove [email protected].

    I will close the question, If you have other questions, please open a new question : )

  • 0
    safi created

    I think the problem is resolved, you can check the code to do the same thing.

    Remove [email protected].

    I will close the question, If you have other questions, please open a new question : )

    ok will create new one but If we are registering already registered user then it's going to redirect on account/register page with already exist message I want show this on my custom login page

  • 0
    liangshiwei created
    Support Team
    //....
    public virtual async Task<IActionResult> OnPostAsync()
    {
        try
        {
            var existsUser = await UserManager.FindByEmailAsync(Input.EmailAddress) ?? await UserManager.FindByNameAsync(Input.UserName);
    
            if (existsUser != null)
            {
                return Redirect("......The Url");
            }
            
    
            //......
            
          }
      }
    
  • 0
    safi created
    //.... 
    public virtual async Task<IActionResult> OnPostAsync() 
    { 
        try 
        { 
            var existsUser = await UserManager.FindByEmailAsync(Input.EmailAddress) ?? await UserManager.FindByNameAsync(Input.UserName); 
     
            if (existsUser != null) 
            { 
                return Redirect("......The Url"); 
            } 
             
     
            //...... 
             
          } 
      } 
    

    Yes, I tried that but it's not showing an already existing message.

  • 0
    safi created
    //.... 
    public virtual async Task<IActionResult> OnPostAsync() 
    { 
        try 
        { 
            var existsUser = await UserManager.FindByEmailAsync(Input.EmailAddress) ?? await UserManager.FindByNameAsync(Input.UserName); 
     
            if (existsUser != null) 
            { 
                return Redirect("......The Url"); 
            } 
             
     
            //...... 
             
          } 
      } 
    

    In catch block already exist message is coming so I added this redirect code

    It's redirecting me to my page but not showing message.

  • 0
    liangshiwei created
    Support Team

    Because redirect will return a 302 HTTP code and told Brower to make a new request, the alert system is not working.

    You can try this:

    Alerts.Danger(GetLocalizeExceptionMessage(e));
    var errorMessage = WebUtility.UrlEncode(GetLocalizeExceptionMessage(e));
    return Redirect("/account/login?Type=register&errorMessage=" + errorMessage);
    
    public class LoginModel:..
    {
        [BindProperty(SupportsGet = true)]
        public string ErrorMessage { get; set; }
        
        public async override Task<IActionResult> OnGetAsync()
        {
            
            if (!ErrorMessage.IsNullOrWhiteSpace())
            {
                Alerts.Danger(ErrorMessage);
            }
            ......
        }
    }
    

    It can solve your problem, but it is not perfect, you better use multiple methods in the Login mode.

    See: https://www.learnrazorpages.com/razor-pages/handler-methods#named-handler-methods

    It will look like this:

    public class LoginModel: ...
    {
         public virtual async Task<IActionResult> OnPostAsync()
         {
             // login...
         }
         
         public virtual async Task<IActionResult> OnPostRegisterAsync()
         {
             // register...
         }
         
         public virtual async Task<IActionResult> OnPost....Async()
         {
             // ...
         }
    }
    
  • 0
    safi created

    Because redirect will return a 302 HTTP code and told Brower to make a new request, the alert system is not working.

    You can try this:

    Alerts.Danger(GetLocalizeExceptionMessage(e)); 
    var errorMessage = WebUtility.UrlEncode(GetLocalizeExceptionMessage(e)); 
    return Redirect("/account/login?Type=register&errorMessage=" + errorMessage); 
    
    public class LoginModel:.. 
    { 
        [BindProperty(SupportsGet = true)] 
        public string ErrorMessage { get; set; } 
         
        public async override Task<IActionResult> OnGetAsync() 
        { 
             
            if (!ErrorMessage.IsNullOrWhiteSpace()) 
            { 
                Alerts.Danger(ErrorMessage); 
            } 
            ...... 
        } 
    } 
    

    It can solve your problem, but it is not perfect, you better use multiple methods in the Login mode.

    See: https://www.learnrazorpages.com/razor-pages/handler-methods#named-handler-methods

    It will look like this:

    public class LoginModel: ... 
    { 
         public virtual async Task<IActionResult> OnPostAsync() 
         { 
             // login... 
         } 
          
         public virtual async Task<IActionResult> OnPostRegisterAsync() 
         { 
             // register... 
         } 
          
         public virtual async Task<IActionResult> OnPost....Async() 
         { 
             // ... 
         } 
    } 
    

    Thanks, it's working perfect.