dotnet / Scaffolding

Code generators to speed up development.
MIT License
633 stars 226 forks source link

.Net 7 Infinite redirect loop after doing Microsoft Identity Scaffolding via documentation.. #2545

Open wrharper opened 11 months ago

wrharper commented 11 months ago

See https://github.com/dotnet/AspNetCore.Docs/issues/30530#issuecomment-1741733636 I was redirected to here. It should have the info needed. Operating system: Visual Studio 2022 Windows 11 pro

I do a standard blazor project serverside prerender and add identity scaffolding. After that, I launch the site and an infinite loop happens because of Login.cshtml/Login.cshtml.cs in Areas\Identity\Pages\Account\

if I delete this it works but all of the forms are broke and don't cache correctly.

database works and it is migrated properly: image

URL proof of infinite loop: https://localhost:44364/Identity/Account/Login?ReturnUrl=%2FIdentity%2FAccount%2FLogin%3FReturnUrl%3D%252FIdentity%252FAccount%252FLogin%253FReturnUrl%253D%25252FIdentity%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FIdentity%2525252FAccount%2525252FLogin%2525253FReturnUrl%2525253D%252525252FIdentity%252525252FAccount%252525252FLogin%252525253FReturnUrl%252525253D%25252525252FIdentity%25252525252FAccount%25252525252FLogin%25252525253FReturnUrl%25252525253D%2525252525252FIdentity%2525252525252FAccount%2525252525252FLogin%2525252525253FReturnUrl%2525252525253D%252525252525252FIdentity%252525252525252FAccount%252525252525252FLogin%252525252525253FReturnUrl%252525252525253D%25252525252525252FIdentity%25252525252525252FAccount%25252525252525252FLogin%25252525252525253FReturnUrl%25252525252525253D%2525252525252525252FIdentity%2525252525252525252FAccount%2525252525252525252FLogin%2525252525252525253FReturnUrl%2525252525252525253D%252525252525252525252FIdentity%252525252525252525252FAccount%252525252525252525252FLogin%252525252525252525253FReturnUrl%252525252525252525253D%25252525252525252525252FIdentity%25252525252525252525252FAccount%25252525252525252525252FLogin%25252525252525252525253FReturnUrl%25252525252525252525253D%2525252525252525252525252FIdentity%2525252525252525252525252FAccount%2525252525252525252525252FLogin%2525252525252525252525253FReturnUrl%2525252525252525252525253D%252525252525252525252525252FIdentity%252525252525252525252525252FAccount%252525252525252525252525252FLogin%252525252525252525252525253FReturnUrl%252525252525252525252525253D%25252525252525252525252525252FIdentity%25252525252525252525252525252FAccount%25252525252525252525252525252FLogin%25252525252525252525252525253FReturnUrl%25252525252525252525252525253D%2525252525252525252525252525252FIdentity%2525252525252525252525252525252FAccount%2525252525252525252525252525252FLogin%2525252525252525252525252525253FReturnUrl%2525252525252525252525252525253D%252525252525252525252525252525252FIdentity%252525252525252525252525252525252FAccount%252525252525252525252525252525252FLogin%252525252525252525252525252525253FReturnUrl%252525252525252525252525252525253D%25252525252525252525252525252525252F

    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
            builder.Services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(connectionString));
            builder.Services.AddDatabaseDeveloperPageExceptionFilter();

            builder.Services.AddDefaultIdentity<ApplicationUser>(options =>
                                             options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();

            builder.Services.AddAuthentication()
               .AddGoogle(options =>
               {
                   IConfigurationSection googleAuthNSection =
                   builder.Configuration.GetSection("Authentication:Google");
                   options.ClientId = googleAuthNSection["ClientId"];
                   options.ClientSecret = googleAuthNSection["ClientSecret"];
               })
               //.AddFacebook(options =>
               //{
               //IConfigurationSection FBAuthNSection =
               //builder.Configuration.GetSection("Authentication:FB");
               //options.ClientId = FBAuthNSection["ClientId"];
               //options.ClientSecret = FBAuthNSection["ClientSecret"];
               //})
               //.AddTwitter(twitterOptions =>
               //{
               //twitterOptions.ConsumerKey = builder.Configuration["Authentication:Twitter:ConsumerAPIKey"];
               //twitterOptions.ConsumerSecret = builder.Configuration["Authentication:Twitter:ConsumerSecret"];
               //twitterOptions.RetrieveUserDetails = true;
               //});
               .AddMicrosoftAccount(microsoftOptions =>
               {
                   microsoftOptions.ClientId = builder.Configuration["Authentication:Microsoft:ClientId"];
                   microsoftOptions.ClientSecret = builder.Configuration["Authentication:Microsoft:ClientSecret"];
               });

            builder.Services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
                options.Secure = CookieSecurePolicy.Always;
            });

            builder.Services.AddControllersWithViews(options =>
            {
                var policy = new AuthorizationPolicyBuilder()
                    .RequireAuthenticatedUser()
                    .Build();
                options.Filters.Add(new AuthorizeFilter(policy));
            });

            // Add services to the container.
            builder.Services.AddRazorPages();
            builder.Services.AddServerSideBlazor();

            builder.Services.AddTransient<IEmailSender, EmailSender>();
            builder.Services.Configure<AuthMessageSenderOptions>(builder.Configuration);

            builder.Services.AddSingleton<WeatherForecastService>();

            var app = builder.Build();

            // Configure the HTTP request pipeline.
            if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

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

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.MapControllers();
            app.MapBlazorHub();
            app.MapFallbackToPage("/_Host");

            app.Run();
        }
    }

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

_Host.cshtml file

@page "/"
@namespace SlayersIsBack.Pages
@{
    Layout = "/Pages/Shared/_Layout.cshtml";
}

<component type="typeof(App)" render-mode="ServerPrerendered" />
wrharper commented 11 months ago

It has been nearly a week, anyone know why this happens?

deepchoudhery commented 11 months ago

Hey, definitely a template issue, will take a look soon. EDIT: PS, localhost urls are not much help, they only work on your local machine.

yammerz commented 11 months ago

When you override these files while also using authorization, you need to [AllowAnonymous] on your LoginModel.

coultonluke commented 5 months ago

When you override these files while also using authorization, you need to [AllowAnonymous] on your LoginModel.

Yes, this is the answer. It must be missing from the template.