首页 > 解决方案 > 尝试在 Web API 项目 ASP.NET Core 3.1 中实现 owin 和承载令牌

问题描述

我正在尝试在我的 ASP.NET Core 3.1 Web API 项目中设置我的 api 的 owin 持有者令牌。我从这里遵循了指南:

https://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/

但是在我的项目中,应用生成器就不一样了:

public void ConfigureOAuth(IAppBuilder app)
{
        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new RoundTableAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
 }

 public Startup(IConfiguration configuration)
 {
     Configuration = configuration;           
 }

// 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.UseSwagger();

        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });

        app.UseRouting();
        // Enable the application to use bearer tokens to authenticate users
        ConfigureOAuth(app);

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
}

然后在我的Configure方法中,我试图调用配置函数,但因为它使用IApplicationBuilder它不兼容;owin 有更新的教程吗?

// 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.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
        });

        app.UseRouting();

        // Enable the application to use bearer tokens to authenticate users
        ConfigureOAuth(app);

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
  }

此外,我似乎AuthRepository在 .net 类中的任何地方都找不到 - 这是第三方的事情吗?

标签: c#asp.net-web-apiowin

解决方案


推荐阅读