locked
VS Code - Authorization failed for user: (null) RRS feed

  • Question

  • User-2089506584 posted

    Hi,

    When running the default Identity scaffolding project from VS Code, I get this error when logging-in;

    VS Code DEBUG CONSOLE:

    ---removed for brevity---
    Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request starting HTTP/1.1 GET http://localhost:48154/ 
    Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed for user: (null).
    info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
    Authorization failed for user: (null).
    info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
    Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
    Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
    info: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
    Executing ChallengeResult with authentication schemes ().
    Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes ().
    info: Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler[12]
    AuthenticationScheme: Identity.Application was challenged.
    Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationHandler:Information: AuthenticationScheme: Identity.Application was challenged.
    info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
    ---removed for brevity---



    I already installed the extension MSSQL, which I'm able to fetch records and viewable within the Menu panel (toolbar).

    However, running the same project from Visual Studio 2019 Community, I can login just fine with no issue.

    Any VS Code extension I'm missing?


    Thanks,

    Sunday, August 9, 2020 1:52 AM

All replies

  • User-1330468790 posted

    Hi imperialx,

     

    It looks like your project missing the authentication middleware: app.UseAuthentication().

    I am just guessing since you don't post any code for the configuration. Could you please share the codes in startup.cs?

    You could refer to this SO link: Authorization failed for user: (null) 

      

    Please be sure to call UseAuthentication() before calling UseMvc() in Startup.Configure()

    Regarding the authentication middleware : please refer to Auth 2.0 Migration announcement

     

    Hope this can help you.

    Best regards,

    Sean

    Monday, August 10, 2020 7:46 AM
  • User-2089506584 posted

    Hi Sean,

    I am just guessing since you don't post any code for the configuration. Could you please share the codes in startup.cs?

    public class Startup
        {
            public IConfiguration Configuration { get; }
    
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")),
                    ServiceLifetime.Transient);
    
                services.AddIdentity<ApplicationUser, IdentityRole>(config =>
                {
                    config.User.RequireUniqueEmail = true;
    
                    config.Password.RequireDigit = false;
                    config.Password.RequiredLength = 6;
            
                })
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
    
                services.Configure<IdentityOptions>(options =>
                {
                    // Password settings
                    options.Password.RequireDigit = true;
                    options.Password.RequiredLength = 6;
    
                    // Lockout settings
                    options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(60);
                    options.Lockout.AllowedForNewUsers = true;
    
                    // User settings
                    options.User.RequireUniqueEmail = true;
                });
    
                services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
                services.AddScoped(typeof(ApplicationContext)); 
                services.AddScoped(typeof(DbContextOptions<ApplicationContext>)); 
    
                //register the services
                services.AddTransient<IRentACarServices, RentACarServices>();
               
                services.AddMvc();
    
                services.AddSingleton<IMemoryCache, MemoryCache>();
                services.AddSession();
                services.AddMemoryCache();
    
                services.AddAutoMapper(); 
            }
    
            // 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.UseBrowserLink();
                    app.UseDeveloperExceptionPage();
                    app.UseDatabaseErrorPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                app.UseAuthentication();
    
                app.UseStaticFiles();
    
                app.UseSession(new SessionOptions()
                {
                    IdleTimeout = new TimeSpan(72, 0, 0) //72 = 3 days
                });
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute("PointOfAccess", "rent/point/{id}",
                        defaults: new { controller = "Rent", action = "Index" },
                        constraints: new { id = @"\d+" });
                });
            }
        }
    I still have the same issue even calling UseAuthentication() before UseMVC().
    Thanks,

    Tuesday, August 11, 2020 4:15 AM