none
asp.net core identity not working RRS feed

  • Question

  • I'm putting together my first MVC asp.net core web applicatoin and having little trouble with the ability to stay logged into the application.  i've added the built in scaffolding login pages and able to immediatlly login.  The problem is as soon as i navigate to another page after logging in it doesnt seem to keep my session or cookie logged in, and this is only after a few seconds.  Any guidance is asspreciated.

    Michael Duhon

    • Moved by CoolDadTx Thursday, February 20, 2020 3:31 PM ASP.NET related
    Wednesday, February 19, 2020 11:21 PM

All replies

  • Hello,

    There is a forum for this https://forums.asp.net/1255.aspx/1?ASP+NET+Core


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, February 20, 2020 2:04 AM
  • I downloaded and ripped out the code from this tutorial except for the Roles logic, and it works like a champ.

    https://www.c-sharpcorner.com/article/getting-started-with-asp-net-core-2-0-identity-and-role-management/

    You can still post to ASP.NET forums

    Thursday, February 20, 2020 5:16 AM
  • Here is my code in my startup configureService 

    services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("TestConnection")));
    services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddUserManager<UserManager<ApplicationUser>>().AddDefaultTokenProviders();
     services.AddAutoMapper(Assembly.GetExecutingAssembly());

                services.AddScoped<INoteRepository, NoteRepository>();
                services.AddScoped<IPatientRepository, PatientRepository>();
                services.AddScoped<ILOCRepository, LOCRepository>();
                services.AddScoped<IAssessmentRepository, AssessmentRepository>();

                services.AddHttpContextAccessor();
                services.AddSession();
                services.AddControllersWithViews();

                services.AddRazorPages();           
                services.AddMvc();

    and here is the code for the Configure in startup

      if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/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.UseSession();
                app.UseRouting();   
                app.UseCookiePolicy();
              

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

                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                    endpoints.MapRazorPages();

                });

                CreateUserRoles(services).Wait();

    I put this code below on my home index page where i get routed once logged in.  When i'm redirected there it determines i'm logged in, but if i immediately refresh the page then i'm not authenticated??

    @if (User.Identity.IsAuthenticated)
    {
        <div>User is authenticated!</div>   
    }
    else
    {
        <div>User is not authenticated!</div>   
    }


    Michael Duhon

    Monday, February 24, 2020 4:17 AM