带有Bearer令牌和Facebook身份验证的WebApi2。在IAppBuilder属性中找不到SignInAsAuthenticationType的默认值

时间:2018-12-10 11:43:48

标签: asp.net-web-api2 facebook-authentication bearer-token

使用Bearer Token OAuth配置Web API 2项目后,一切工作正常。我设法通过/令牌获得令牌。当我添加Facebook身份验证时,尽管它开始提示“在SignInAsAuthenticationType属性中找不到IAppBuilder的默认值”。错误。我保证我已经为令牌和Facebook身份验证添加了SignInAsAuthentication类型,并且这种类型还在继续。我在哪里错了?

public void ConfigureAuth(IAppBuilder app)
    {
        _dataProtectionProvider = app.GetDataProtectionProvider();
        IUnityContainer container = UnityBootstrapper.Container;
        container.RegisterInstance(DataProtectionProvider);

        BootstrapperUnityResolver dependencyResolver = new BootstrapperUnityResolver(container);

        // Configure the db context and user manager to use a single instance per request
        app.CreatePerOwinContext(() => ApplicationDbContext.Create());
        app.CreatePerOwinContext(() => (ApplicationUserManager)dependencyResolver.GetService(typeof(ApplicationUserManager)));
        app.CreatePerOwinContext(() => (ApplicationSignInManager)dependencyResolver.GetService(typeof(ApplicationSignInManager)));

        PublicClientId = "self";
        OAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/Token"),
            AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),
            Provider = new ApplicationOAuthProvider(PublicClientId),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30)
        };
        app.SetDefaultSignInAsAuthenticationType(OAuthDefaults.AuthenticationType);

        //app.UseOAuthBearerTokens(OAuthServerOptions);

        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        OAuthAuthenticationOptions = new OAuthBearerAuthenticationOptions
        {
            Provider = new OAuthBearerAuthenticationProvider()
        };
        app.UseOAuthBearerAuthentication(OAuthAuthenticationOptions);

        var facebookAuthenticationOptions = new FacebookAuthenticationOptions();
        facebookAuthenticationOptions.AppId = "***";
        facebookAuthenticationOptions.AppSecret = "*****";
        facebookAuthenticationOptions.Provider = new FacebookAuthenticationProvider()
        {
            OnAuthenticated = async ctx =>
            {
                var token = ctx.AccessToken;

                var id = ctx.Id;
                var email = ctx.User["email"];

                ctx.Identity.AddClaim(new Claim("urn:myWebSite:AuthorityId", "1", ClaimValueTypes.String, "Facebook"));

                if (id != null)
                    ctx.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, id.ToString(), ClaimValueTypes.String, "Facebook"));
                if (email != null)
                    ctx.Identity.AddClaim(new Claim(ClaimTypes.Email, email.ToString(), ClaimValueTypes.String, "Facebook"));

                ctx.Identity.AddClaim(new Claim("fb.token", token));
            }
        };

        facebookAuthenticationOptions.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalBearer;
        app.UseFacebookAuthentication(facebookAuthenticationOptions);
    }

0 个答案:

没有答案