Home > Blockchain >  ASP.NET Core MVC : add OAuth missing Startup.CS OWIN
ASP.NET Core MVC : add OAuth missing Startup.CS OWIN

Time:11-16

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:

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

CodePudding user response:

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();
  • Related