ASP.NET Core 5 MVC - Google Auth not working - nothing happened after execute login

Viewed 281

Right after I sign in - nothing happened I followed this tutorial: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/google-logins?view=aspnetcore-5.0

I read something about token.. but I don't have any access to it nor it doesn't display on the screen or something. I thought maybe there's an option to do a if() statement inside my .cshtml so I will be able to check if the user is logged in into his google account or not.

For sure, this code doesn't work for Google authentication:

@if ((User.Identity.IsAuthenticated) || (Context.User != null && Context.User.Claims != null && Context.User.Claims.Count() > 0))
                    {
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Categories" asp-action="Index">Categories</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Restaurants" asp-action="Index">Restaurants</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Dishes" asp-action="Index">Dishes</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Users" asp-action="Index">Users</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Cities" asp-action="Index">Cities</a>
                        </li>
                    }
                    else
                    {
                        <li class="nav-item">
                            <a class="nav-link text-dark" type="button" data-bs-toggle="modal" data-bs-target="#LoginModal">Categories</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" type="button" data-bs-toggle="modal" data-bs-target="#LoginModal">Restaurants</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" type="button" data-bs-toggle="modal" data-bs-target="#LoginModal">Dishes</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" type="button" data-bs-toggle="modal" data-bs-target="#LoginModal">Users</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" type="button" data-bs-toggle="modal" data-bs-target="#LoginModal">Cities</a>
                        </li>
                    }


@if ((User.Identity.IsAuthenticated) || (Context.User != null && Context.User.Claims != null && Context.User.Claims.Count() > 0))
                    {
                        <div class="dropdown">
                            <li class="text-dark dropdown-toggle" href="#" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown">
                                Hello, @User.Identity.Name!
                            </li>
                            <ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
                                <li><span class="dropdown-item-text">User Status: @Context.User.Claims.ToList()[1].Value</span></li>
                                <li><a class="dropdown-item" href="#">My Cart</a></li>
                                <li><a class="dropdown-item" href="#">Another action</a></li>
                                <li><a class="dropdown-item" asp-controller="Users" asp-action="Logout">Logout</a></li>
                            </ul>
                        </div>
                    }
                    else
                    {
                        <li class="nav-item nav-link text-dark" type="button" data-bs-toggle="modal" data-bs-target="#LoginModal">
                            Hello, Guest!
                        </li>
                    }

Startup.cs code

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using FeedMe.Data;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;

namespace FeedMe
{
    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.AddControllersWithViews();

            services.AddDbContext<FeedMeContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("FeedMeContext")));

            //services.AddSession(options =>
            //{
            //    options.IdleTimeout = TimeSpan.FromMinutes(10);
            //});

            services.AddDbContext<FeedMeContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("FeedMeContext")));
            services.AddDefaultIdentity<IdentityUser>(options =>
                options.SignIn.RequireConfirmedAccount = true)
                    .AddEntityFrameworkStores<FeedMeContext>();
            services.AddRazorPages();

            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
                .AddCookie(options =>
                {
                    options.LoginPath = "/Users/Login";
                    options.AccessDeniedPath = "/Users/AccessDenied";
                })

                .AddGoogle(options =>
                {
                    options.ClientId = Configuration["Authentication:Google:ClientId"];
                    options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
                    options.AccessDeniedPath = "/Users/AccessDenied";
                })
                ;
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            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.UseRouting();

            //app.UseSession();

            app.UseAuthentication();

            app.UseAuthorization();

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

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "FeedMeContext": "XXXXXX"
  },

  "Authentication": {
    "Google": {
      "ClientId": "XXXX",
      "ClientSecret": "YYYY"
    }
  }
}

But it does work for the regular cookie login&register I built before

0 Answers
Related