1

I was going through Microsoft's documentation on how to add OAuth to a web application (I'm using .NET core) but then I got to this point:

Add the Google service to Startup.ConfigureServices.

I don't have a Startup class, not even a file. While searching for how to deal with it, I found this answer, which drove me to found out that this is a OWIN file (whatever that means) and that I should just right click and add it and everything would be fine. I then proceeded to add the owin packages (this is even redundant, I know):

<PackageReference Include="Microsoft.Owin" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Host.SystemWeb" Version="4.2.0" />
<PackageReference Include="Microsoft.Owin.Security.OAuth" Version="4.2.0" />

Tried then to reload the project, reopen Visual Studio (using Visual Studio 2022 RC) but this class won't show up:

Owin files not found

I just need to add OAuth to my application... Do I really need to do this? If so, what can I do? I just need a functional ConfigureServices method like the documentation shows:

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseSqlServer(
            Configuration.GetConnectionString("DefaultConnection")));
    services.AddDefaultIdentity<IdentityUser>(options =>
        options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>();
    services.AddRazorPages();

    services.AddAuthentication()
        .AddGoogle(options =>
        {
            IConfigurationSection googleAuthNSection =
                Configuration.GetSection("Authentication:Google");

            options.ClientId = googleAuthNSection["ClientId"];
            options.ClientSecret = googleAuthNSection["ClientSecret"];
        });
}

This is how my project is right now:

File organization of the project

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vitor Ceolin
  • 196
  • 17

1 Answers1

2

According to your description and image, I found you created a asp.net 6 project.

Asp.net 6 doesn't contain the startup.cs now. All the DI service should be registered inside the program.cs file.

Besides, the Microsoft.Owin is used for asp.net 4, we couldn't use it inside asp.net 6. Please remove these packages.

More details, you could refer to below steps:

Install the package: Microsoft.AspNetCore.Authentication.Google 6.0.0

Modify the program.cs as below:

var builder = WebApplication.CreateBuilder(args);

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

builder.Services.AddAuthentication()
    .AddGoogle(options =>
    {
        IConfigurationSection googleAuthNSection =
             builder.Configuration.GetSection("Authentication:Google");

        options.ClientId = googleAuthNSection["ClientId"];
        options.ClientSecret = googleAuthNSection["ClientSecret"];
    });

var app = builder.Build();



// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    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.UseAuthentication();    
app.UseRouting();

app.UseAuthorization();

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

app.Run();
Brando Zhang
  • 22,586
  • 6
  • 37
  • 65