Owin在MVC Web API中提供CancellationToken异常

环境:.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()



喵喔喔
浏览 217回答 1
1回答

杨魅力

首先让我简单介绍一下OWIN的工作原理。值得您阅读更多有关它的信息,但这是一个很好的起点。将OWIN视为请求和应用程序之间的管道,本例中为MVC Web Api。流水线的每个部分都称为“中间件”,请求流经此流水线,直到到达您的应用程序为止。当请求到达管道时,流程反向,数据流回通过管道。因此,当数据进入应用程序以及何时离开应用程序时,该管道的每个部分(“中间件”)都会看到两次数据。中间件可以在请求进入时查看或修改请求,或者在离开应用程序时查看或修改响应,或两者。每个中间件组件都接收一个字典,其中包含有关请求和响应的各种数据,以及一个调用下一个管道的委托。现在为您的代码:您的应用程序是WebApi,由以下几行定义:app.UseWebApi(AppConfiguration);您可以使用以下几行来初始化两个中间件:&nbsp; &nbsp; var issuer = ConfigurationManager.AppSettings["as:IssuerServer"];&nbsp; &nbsp; var tokenEndpoint = ConfigurationManager.AppSettings["as:OwinTokenEndpoint"];&nbsp; &nbsp; // "/oauth/token"&nbsp; &nbsp; OAuthAuthorizationServerOptions oAuthServerOptions = new OAuthAuthorizationServerOptions()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ////For Dev enviroment only (on production should be AllowInsecureHttp = false)&nbsp; &nbsp; &nbsp; &nbsp; AllowInsecureHttp = true,&nbsp; &nbsp; &nbsp; &nbsp; TokenEndpointPath = new Microsoft.Owin.PathString(tokenEndpoint),&nbsp; &nbsp; &nbsp; &nbsp; AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),&nbsp; &nbsp; &nbsp; &nbsp; Provider = new CustomOAuthProvider(),&nbsp; &nbsp; &nbsp; &nbsp; AccessTokenFormat = new CustomJwtFormat(issuer)&nbsp; &nbsp; };&nbsp; &nbsp; // OAuth 2.0 Bearer Access Token Generation&nbsp; &nbsp; this.app.UseOAuthAuthorizationServer(oAuthServerOptions);然后这些行:&nbsp; &nbsp; string audienceId = ConfigurationManager.AppSettings["as:AudienceId"];&nbsp; &nbsp; var issuer = ConfigurationManager.AppSettings["as:IssuerServer"]; // Should have the Url of the auth server http://localhost:53025/";&nbsp; &nbsp; byte[] audienceSecret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["as:AudienceSecret"]);&nbsp; &nbsp; this.app.UseJwtBearerAuthentication(&nbsp; &nbsp; &nbsp; &nbsp; new JwtBearerAuthenticationOptions&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AuthenticationMode = AuthenticationMode.Active,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; AllowedAudiences = new[] { audienceId },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IssuerSecurityKeyProviders = new IIssuerSecurityKeyProvider[]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new SymmetricKeyIssuerSecurityKeyProvider(issuer, audienceSecret)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });该中间件将始终运行。不管您是否在任何地方都有任何属性。您说该错误在本地开发计算机上不会发生,这意味着您很可能遇到配置问题。您正在以上引用的行中将设置传递到中间件中。基于此,我怀疑您对issuer或tokenEndpoint变量或(更可能是)audienceId或audienceSecret变量有问题。希望能有所帮助。
打开App,查看更多内容
随时随地看视频慕课网APP