为了账号安全,请及时绑定邮箱和手机立即绑定

Owin在MVC Web API中提供CancellationToken异常

Owin在MVC Web API中提供CancellationToken异常

C#
喵喔喔 2021-05-04 15:16:31
环境:.NET 4.6.1,ASP.NET MVC 2,Microsoft.Owin应用情况Owin配置为基于OAuth的身份验证(自己的数据库)。未添加授权属性。是下一步代码库启动public void Configuration(IAppBuilder app)    {        OAuthConfig oAuthConfig = new OAuthConfig(app, AppConfiguration);        oAuthConfig.ConfigureOAuthTokenGeneration();        oAuthConfig.ConfigureOAuthTokenConsumption();        WebApiConfig.Register(AppConfiguration);        app.UseWebApi(AppConfiguration);        // No further configuration is now allowed.         AppConfiguration.EnsureInitialized();    }OAuthConfig    public OAuthConfig(IAppBuilder app, HttpConfiguration HttpConfiguration)    {        this.app = app;        if (OAuthConfig.HttpConfiguration == null)            OAuthConfig.HttpConfiguration = HttpConfiguration;        this.app.Use(async (ctx, next) =>        {            try            {                await next();            }            catch (OperationCanceledException)            {                // Eat this exception            }        });    }    public void ConfigureOAuthTokenGeneration()    {        var userStore = HttpConfiguration.DependencyResolver.GetService(typeof(IUserStore<ExtendedUser, string>)) as IUserStore<ExtendedUser, string>;        UserService.UserStore = userStore;        this.app.CreatePerOwinContext<UserService>(UserService.Create);        this.app.CreatePerOwinContext<SignInService>(SignInService.Create);        var issuer = ConfigurationManager.AppSettings["as:IssuerServer"];        var tokenEndpoint = ConfigurationManager.AppSettings["as:OwinTokenEndpoint"];    // "/oauth/token"        OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions()
查看完整描述

1 回答

?
杨魅力

TA贡献1811条经验 获得超5个赞

首先让我简单介绍一下OWIN的工作原理。值得您阅读更多有关它的信息,但这是一个很好的起点。


将OWIN视为请求和应用程序之间的管道,本例中为MVC Web Api。流水线的每个部分都称为“中间件”,请求流经此流水线,直到到达您的应用程序为止。当请求到达管道时,流程反向,数据流回通过管道。因此,当数据进入应用程序以及何时离开应用程序时,该管道的每个部分(“中间件”)都会看到两次数据。中间件可以在请求进入时查看或修改请求,或者在离开应用程序时查看或修改响应,或两者。


每个中间件组件都接收一个字典,其中包含有关请求和响应的各种数据,以及一个调用下一个管道的委托。


现在为您的代码:


您的应用程序是WebApi,由以下几行定义:


app.UseWebApi(AppConfiguration);

您可以使用以下几行来初始化两个中间件:


    var issuer = ConfigurationManager.AppSettings["as:IssuerServer"];

    var tokenEndpoint = ConfigurationManager.AppSettings["as:OwinTokenEndpoint"];    // "/oauth/token"

    OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions()

    {

        ////For Dev enviroment only (on production should be AllowInsecureHttp = false)

        AllowInsecureHttp = true,

        TokenEndpointPath = new Microsoft.Owin.PathString(tokenEndpoint),

        AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),

        Provider = new CustomOAuthProvider(),

        AccessTokenFormat = new CustomJwtFormat(issuer)

    };


    // OAuth 2.0 Bearer Access Token Generation

    this.app.UseOAuthAuthorizationServer(oAuthServerOptions);

然后这些行:


    string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];

    var issuer = ConfigurationManager.AppSettings["as:IssuerServer"]; // Should have the Url of the auth server http://localhost:53025/";

    byte[] audienceSecret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["as:AudienceSecret"]);

    this.app.UseJwtBearerAuthentication(

        new JwtBearerAuthenticationOptions

        {

            AuthenticationMode = AuthenticationMode.Active,

            AllowedAudiences = new[] { audienceId },

            IssuerSecurityKeyProviders = new IIssuerSecurityKeyProvider[]

            {

                new SymmetricKeyIssuerSecurityKeyProvider(issuer, audienceSecret)

            }

        });

该中间件将始终运行。不管您是否在任何地方都有任何属性。


您说该错误在本地开发计算机上不会发生,这意味着您很可能遇到配置问题。您正在以上引用的行中将设置传递到中间件中。


基于此,我怀疑您对issuer或tokenEndpoint变量或(更可能是)audienceId或audienceSecret变量有问题。


希望能有所帮助。


查看完整回答
反对 回复 2021-05-16
  • 1 回答
  • 0 关注
  • 214 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信