【发布时间】:2019-12-28 18:04:44
【问题描述】:
我正在尝试使用 this tutorial 将 OAuth 2.0 添加到我的 .NET Core 3.0 Web Api。以下是WebApiConfig类的内容。
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute( // HERE IS THE ERROR
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// WebAPI when dealing with JSON & JavaScript!
// Setup json serialization to serialize classes to camel (std. Json format)
var formatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
formatter.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver();
// Adding JSON type web api formatting.
config.Formatters.Clear();
config.Formatters.Add(formatter);
}
我猜它与依赖关系有关,所以这里是 csproj:
<ItemGroup>
<PackageReference Include="EntityFramework" Version="6.4.0" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNet.WebApi.Core" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNet.WebApi.Owin" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNet.WebApi.WebHost" Version="5.2.7" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.0.0" />
<PackageReference Include="Microsoft.Owin.Cors" Version="4.1.0" />
<PackageReference Include="Microsoft.Owin.Security.OAuth" Version="4.1.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
我看到了一个类似的问题,其中一个答案建议使用 app.UseMvc 来映射路线,但他使用的是不同的 .NET 版本 (2.0)。我也尝试使用 app.UseMvc 但它什么也没做,因为我使用 EndpointRouting。
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.AddIdentity<AppUser, AppRole>(options =>
{
options.User.RequireUniqueEmail = true;
}).AddEntityFrameworkStores<AppDbContext>();
services.AddControllers();
//+adding database and service/repository
}
// 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();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseAuthentication();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
所以我不太确定该怎么做。我尝试关闭 Visual Studio 并删除项目中的每个 obj、bin 和 vs 文件夹(听起来很奇怪,有些人认为它可以工作)但没有成功。
有什么建议吗?
【问题讨论】:
-
教程太老了,是针对asp.net web api而不是asp.net core web api的。所以有些包不适合asp.net core 3.0项目。你可以搜索其他教程准确的asp.net核心。使用OAuth,参考jerriepelser.com/blog/authenticate-oauth-aspnet-core-2外部登录,参考docs.microsoft.com/en-us/aspnet/core/security/authentication/…
标签: c# asp.net-web-api oauth asp.net-core-3.0