Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Thinktecture.IdentityModel lib在控制器的标识中使用Webapi和OWIN的UseBasicAuthentication没有任何声明_C#_Asp.net Web Api_Owin_Thinktecture Ident Model_Thinktecture - Fatal编程技术网

C# 使用Thinktecture.IdentityModel lib在控制器的标识中使用Webapi和OWIN的UseBasicAuthentication没有任何声明

C# 使用Thinktecture.IdentityModel lib在控制器的标识中使用Webapi和OWIN的UseBasicAuthentication没有任何声明,c#,asp.net-web-api,owin,thinktecture-ident-model,thinktecture,C#,Asp.net Web Api,Owin,Thinktecture Ident Model,Thinktecture,我正在使用Thinktecture.IdentityModel,并尝试将Owin.BasicAuthentication库与Webapi和Owin的UseBasicAuthentication一起使用。我的控制器中的标识没有声明,并且显示未经验证 我在Startup.Auth.cs中设置了owin配置 app.SetDefaultSignInAsAuthenticationType("Basic"); //app.Use(typeof (BasicAuthent

我正在使用Thinktecture.IdentityModel,并尝试将Owin.BasicAuthentication库与Webapi和Owin的UseBasicAuthentication一起使用。我的控制器中的标识没有声明,并且显示未经验证

我在Startup.Auth.cs中设置了owin配置

        app.SetDefaultSignInAsAuthenticationType("Basic");

        //app.Use(typeof (BasicAuthentication), new[] {_container.Resolve<UserAccountService>()});
        app.UseBasicAuthentication(new BasicAuthenticationOptions("realm", ValidationFunction)
        {
            AuthenticationType = "Basic",
            AuthenticationMode = AuthenticationMode.Active
        });

        var oauthServerConfig = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            Provider = new MembershipRebootProvider(_container.Resolve<UserAccountService>()),
            TokenEndpointPath = new PathString("/token")
        };
        app.UseOAuthAuthorizationServer(oauthServerConfig);

        var oauthConfig = new OAuthBearerAuthenticationOptions
        {
            AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
            AuthenticationType = "Bearer"
        };
        app.UseOAuthBearerAuthentication(oauthConfig);

    private static Task<IEnumerable<Claim>> ValidationFunction(string userName, string password)
    {
        IEnumerable<Claim> claims = null;
        UserAccount user;
        string tenant = "";

        if (userName.Contains("\\"))
        {
            var parts = userName.Split('\\');
            tenant = parts[0];
            userName = parts[1];
        }
        else
        {
            throw new Exception("Cannot determine tenant and username.");
        }

        var userAccountService = _container.Resolve<UserAccountService>();
        if (userAccountService.Authenticate(tenant, userName, password, out user))
        {
            claims = user.GetAllClaims();
        }

        return Task.FromResult(claims);
    }

我遗漏了什么?

我在配置中确实有suppress SuppressDefaultHostAuthentication,但在检查此项时,我注意到我没有仅针对“Basic”Oauth BealerToken的筛选器。我添加了这个,现在它工作了

        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        config.Filters.Add(new HostAuthenticationFilter("Basic"));
        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
        config.Filters.Add(new HostAuthenticationFilter("Basic"));