【问题标题】:Using Google.Apis.Auth results in a unique exception in .Net project使用 Google.Apis.Auth 会导致 .Net 项目中的唯一异常
【发布时间】:2021-10-05 08:03:29
【问题描述】:

美好的一天! 我正在尝试将 Google 身份验证添加到 Firestore 项目。 使用了各种捆绑包 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”加载类型“System.Web.HttpContextBase”,版本 = 4.0.0.0,文化 = 中性,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.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("/");
        }
    }
}

程序.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.Controllercontroller.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; }
    }
}

【问题讨论】:

  • 添加对 System.Web.HttpContextBase 的引用
  • 非常感谢!我想我明白错误是什么

标签: c# .net-core google-cloud-firestore google-api


【解决方案1】:

我的错误是在 .Net Core 项目中使用 Google.Apis.Auth.OAuth2.Flows。
您需要使用 Google.Apis.Auth.AspNetCore3 库。 An example of how to use it

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    相关资源
    最近更新 更多