在.net项目中使用Google.Apis.Auth会导致一个唯一的异常



大家好!我正试图将谷歌认证添加到Firestore项目。使用了各种捆绑包VS2017, VS2019, .net Core 2.2, 3.1, .net 5.0。在提到Google.Apis.Auth.OAuth2.Mvc之前,整个项目运行良好。添加控制器(或AspNetCore3)。一次简单的提到添加Startup.cs

app.UseEndpoints (...);

抛出异常系统。typeeloadeexception: "无法加载类型'System.Web。HttpContextBase从装配系统。Web, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a'.">使用的依赖项:

<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。api, Google.Apis。身份验证,Google.Apis.Auth。Mvc, Google.Apis。

有以下例外的小代码: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控制器。会话是错误"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; }
}
}

我的错误是使用Google.Apis.Auth.OAuth2。流在中。网络核心项目。
需要使用google . api . auth .AspNetCore3图书馆。一个如何使用它的例子

相关内容

最新更新