使用 Google.Apis.Auth 会导致 .Net 项目中的唯一异常
Using Google.Apis.Auth results in a unique exception in .Net project
美好的一天!
我正在尝试向 Firestore 项目添加 Google 身份验证。
使用各种捆绑包 VS2017、VS2019、.net Core 2.2、3.1、.net 5.0。
在添加 Google.Apis.Auth.OAuth2.Mvc.Controllers (或 AspNetCore3) 之前,整个项目工作正常。
一旦在 Startup.cs
中添加了一个简单的提及
app.UseEndpoints (...);
抛出异常
System.TypeLoadException:“无法从程序集 'System.Web, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a' 加载类型 'System.Web.HttpContextBase'。”
使用的依赖项:
<PackageReference Include="Firebase.Auth" Version="1.0.0" />
<PackageReference Include="FirebaseDatabase.net" Version="4.0.6" />
<PackageReference Include="Google.Apis.Auth.AspNetCore3" Version="1.55.0" />
<PackageReference Include="Google.Cloud.Firestore" Version="2.4.0" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
我尝试添加Google.Apis、Google.Apis.Auth、Google.Apis.Auth.Mvc、Google.Apis.Core,结果是一样的。
带有此异常的小代码:
AuthCallbackController.cs
using System.Web.Mvc;
namespace Google.Apis.Auth.OAuth2.Mvc.Controllers
{
public class AuthCallbackController : Controller
{
public ActionResult Index()
{
return Redirect("/");
}
}
}
Program.cs
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace ToDo2019Help
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ToDo2019Help
{
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.AddRazorPages();
}
// 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("/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.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
最终工程中代码较多,但在app.UseEndpoints上仍然抛出同样的异常。我很抱歉英语不好。感谢您的帮助!
编辑
如果在AuthCallbackController.cs
中替换
using System.Web.Mvc;
同线
using Microsoft.AspNetCore.Mvc;
不再抛出这个异常,
但是 GetUserId 需要 System.Web.Mvc.Controller 并且 controller.Session 是
错误“CS7069。引用对类型“HttpSessionStateBase”的引用需要它在“System.Web”中的定义,但找不到它
AppFlowMetadata.cs
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
public class AppFlowMetadata : Google.Apis.Auth.OAuth2.Mvc.FlowMetadata
{
private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "PPPPPPPP",
ClientSecret = "PPPPPPPPP"
},
Scopes = new[] { "email", "profile" },
});
public override string GetUserId(System.Web.Mvc.Controller controller)
{
var user = controller.Session["user"];
if (user == null)
{
user = System.Guid.NewGuid();
controller.Session["user"] = user;
}
return user.ToString();
throw new System.NotImplementedException();
}
public override string AuthCallback
{
get
{
return @"/AuthCallback/IndexAsync";
}
}
public override IAuthorizationCodeFlow Flow
{
get { return flow; }
}
}
我的错误是在 .Net Core 项目中使用 Google.Apis.Auth.OAuth2.Flows。
您需要使用 Google.Apis.Auth.AspNetCore3 库。 An example of how to use it
美好的一天! 我正在尝试向 Firestore 项目添加 Google 身份验证。 使用各种捆绑包 VS2017、VS2019、.net Core 2.2、3.1、.net 5.0。 在添加 Google.Apis.Auth.OAuth2.Mvc.Controllers (或 AspNetCore3) 之前,整个项目工作正常。 一旦在 Startup.cs
中添加了一个简单的提及app.UseEndpoints (...);
抛出异常 System.TypeLoadException:“无法从程序集 'System.Web, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a' 加载类型 'System.Web.HttpContextBase'。” 使用的依赖项:
<PackageReference Include="Firebase.Auth" Version="1.0.0" />
<PackageReference Include="FirebaseDatabase.net" Version="4.0.6" />
<PackageReference Include="Google.Apis.Auth.AspNetCore3" Version="1.55.0" />
<PackageReference Include="Google.Cloud.Firestore" Version="2.4.0" />
<PackageReference Include="Microsoft.AspNet.Mvc" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.10" />
<PackageReference Include="Microsoft.AspNetCore.Session" Version="2.2.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.2" />
我尝试添加Google.Apis、Google.Apis.Auth、Google.Apis.Auth.Mvc、Google.Apis.Core,结果是一样的。
带有此异常的小代码: AuthCallbackController.cs
using System.Web.Mvc;
namespace Google.Apis.Auth.OAuth2.Mvc.Controllers
{
public class AuthCallbackController : Controller
{
public ActionResult Index()
{
return Redirect("/");
}
}
}
Program.cs
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace ToDo2019Help
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
}
Startup.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace ToDo2019Help
{
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.AddRazorPages();
}
// 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("/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.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
最终工程中代码较多,但在app.UseEndpoints上仍然抛出同样的异常。我很抱歉英语不好。感谢您的帮助!
编辑
如果在AuthCallbackController.cs
中替换using System.Web.Mvc;
同线
using Microsoft.AspNetCore.Mvc;
不再抛出这个异常, 但是 GetUserId 需要 System.Web.Mvc.Controller 并且 controller.Session 是 错误“CS7069。引用对类型“HttpSessionStateBase”的引用需要它在“System.Web”中的定义,但找不到它
AppFlowMetadata.cs
using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Flows;
public class AppFlowMetadata : Google.Apis.Auth.OAuth2.Mvc.FlowMetadata
{
private static readonly IAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "PPPPPPPP",
ClientSecret = "PPPPPPPPP"
},
Scopes = new[] { "email", "profile" },
});
public override string GetUserId(System.Web.Mvc.Controller controller)
{
var user = controller.Session["user"];
if (user == null)
{
user = System.Guid.NewGuid();
controller.Session["user"] = user;
}
return user.ToString();
throw new System.NotImplementedException();
}
public override string AuthCallback
{
get
{
return @"/AuthCallback/IndexAsync";
}
}
public override IAuthorizationCodeFlow Flow
{
get { return flow; }
}
}
我的错误是在 .Net Core 项目中使用 Google.Apis.Auth.OAuth2.Flows。
您需要使用 Google.Apis.Auth.AspNetCore3 库。 An example of how to use it