Feedback
Did this article resolve your question/issue?

   

Article

MVC: Redirect to custom page after login

« Go Back

Information

 
TitleMVC: Redirect to custom page after login
URL NameMVC-Redirect-to-custom-page-after-login
Article Number000179604
EnvironmentProduct: Sitefinity
Version: 7.3, 8.x, 9.x, 10.x,11.x, 12.x, 13.x
OS: All supported OS versions
Database: All supported Microsoft SQL Server versions
Question/Problem Description
How to set a custom redirect page after login based on the user logging in?
How to change the return Url of the MVC login form dynamically based on the user or provider?
Steps to Reproduce
Clarifying Information
The MVC login form widget has out of the box option to redirect after login to a page in Sitefinity.
Error Message
Defect Number
Enhancement Number
Cause
Resolution
This can be set up in the Login widget Model. It is functionally correct to be there since the custom functionality will be for the MVC Feather LoginForm widget.

Create the custom Model inheriting from the default one and implement the redirect needed, for example:

For Sitefinity version up to and including Sitefinity 9.x
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Frontend.Identity.Mvc.Models.LoginForm;

namespace SitefinityWebApp.Mvc.Models
{
    public class LoginFormModelCustom : LoginFormModel
    {
        public override LoginFormViewModel Authenticate(LoginFormViewModel input, HttpContextBase context)
        {
            var result = base.Authenticate(input, context);
            if (!result.IncorrectCredentials)
            {
                input.RedirectUrlAfterLogin = this.GetRedirectUrl(input.UserName, input.MembershipProvider);
            }

            return result;
        }

        private string GetRedirectUrl(string userName, string provider)
        {
            return "http://sitefinity.com";
        }
    }
}


For Sitefinity versions later than and including Sitefinity 10.x:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Telerik.Sitefinity.Frontend.Identity.Mvc.Models.LoginForm;

namespace SitefinityWebApp.Mvc.Models
{
    public class LoginFormModelCustom : LoginFormModel
    {
        public override LoginFormViewModel Authenticate(LoginFormViewModel input, HttpContextBase context)
        {
            input.LoginError = false;
        
            if (Config.Get<SecurityConfig>().AuthenticationMode == AuthenticationMode.Claims)
            {
                var owinContext = context.Request.GetOwinContext();
                var challengeProperties = ChallengeProperties.ForLocalUser(input.UserName, input.Password, this.MembershipProvider, input.RememberMe, context.Request.Url.ToString());
                //call the custom method here to change the redirect url
                challengeProperties.RedirectUri = GetRedirectUrl(input.UserName,this.MembershipProvider);//this.GetReturnURL(context);
                owinContext.Authentication.Challenge(challengeProperties, ClaimsManager.CurrentAuthenticationModule.STSAuthenticationType);
            }
            else
            {
                User user;
                UserLoggingReason result = SecurityManager.AuthenticateUser(this.MembershipProvider,input.UserName,input.Password,input.RememberMe,out user);
            
                if (result != UserLoggingReason.Success)
                {
                    input.LoginError = true;
                }
                else
                {
                    //call the custom method here to change the redirect url
                    input.RedirectUrlAfterLogin = GetRedirectUrl(input.UserName, this.MembershipProvider);
                }
            }
            
            return input;
        }

        private string GetRedirectUrl(string userName, string provider)
        {
            return "http://sitefinity.com";
        }
    }
}


To rebind the default model with the custom one, go to the Global.asax (create one if not already created - new -> Global Application class). Use the code below
public class Global : System.Web.HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        Bootstrapper.Bootstrapped += this.Bootstrapper_Bootstrapped;
    }

    private void Bootstrapper_Bootstrapped(object sender, EventArgs e)
    {
        FrontendModule.Current.DependencyResolver.Rebind<ILoginFormModel>().To<LoginFormModelCustom>();
    }
}

 
Workaround
Notes
Based on this article:
Sitefinity documentation, Extend the Navigation widget model http://docs.sitefinity.com/feather-extend-the-navigation-widget-model
Last Modified Date8/13/2021 10:07 PM
Attachment 
Files
Disclaimer The origins of the information on this site may be internal or external to Progress Software Corporation (“Progress”). Progress Software Corporation makes all reasonable efforts to verify this information. However, the information provided is for your information only. Progress Software Corporation makes no explicit or implied claims to the validity of this information.

Any sample code provided on this site is not supported under any Progress support program or service. The sample code is provided on an "AS IS" basis. Progress makes no warranties, express or implied, and disclaims all implied warranties including, without limitation, the implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample code is borne by the user. In no event shall Progress, its employees, or anyone else involved in the creation, production, or delivery of the code be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample code, even if Progress has been advised of the possibility of such damages.