Friendly URL in ASP.NET Core Identity

1

I am developing an application using ASP.NET Identity for managing the login. Within the Login.cshtml page of ASP.NET identity I want to send another parameter that is the name of the company. The problem is that I require it to be a friendly URL for the user.

For example, it works like this right now:

https://localhost:44311/Identity/Account/Login?company=EmpresaPrueba

But I'd like you to stay:

https://localhost:44311/Identity/Account/Login/EmpresaPrueba

Some colleagues idea.

I tried the Route attribute within the method

public async Task OnGetAsync(string company=null)

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure<CookiePolicyOptions>(options =>
            {
                // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });



            services.AddDbContext<Data.DbContext>(options =>
                options.UseLazyLoadingProxies().UseSqlServer(
                    Configuration.GetConnectionString("Authentication")));

            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddDefaultUI()
                .AddDefaultTokenProviders()
                .AddEntityFrameworkStores<Data.DbContext>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseAuthentication();

            app.UseMvc(routes =>
            {



            routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });




        }
    }
}
    
asked by Samuel Andreé Arellano Díaz 18.09.2018 в 22:27
source

2 answers

1

My solution was this ... maybe it is not a very subtle and fine way to do it, but for now the redirection works

services.AddMvc().AddRazorPagesOptions(
            o =>
            o.Conventions.AddAreaFolderRouteModelConvention("Identity", "/Account/",
            model =>
            {
                foreach (var selector in model.Selectors)
                {
                    var attributeRouteModel = selector.AttributeRouteModel;
                    attributeRouteModel.Order = -1;
                    if (attributeRouteModel.Template == "Identity/Account/Login")
                    {

                        attributeRouteModel.Template = "Account/Login/{company?}";


                    }
                    else
                    {
                        attributeRouteModel.Template =
                        attributeRouteModel.Template.Remove(0, "Identity".Length);
                    }

                }
            })
            ).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
answered by 19.09.2018 в 20:27
0

Try something like this:

app.UseMvc(routes => {

    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
     });

    routes.MapRoute(
        name: "IdentityLoginRoute",
        template: "Account/Login/{company}",
        defaults : new{controller = "Account", action = "Login"
    });
}

And in the login controller:

public IActionResult Login(string company)
{
   //Aqui va el código que tengas que tener :D
}
    
answered by 18.09.2018 в 23:36