我正在使用 Thinktecture.IdentityModel 并尝试将 Owin.BasicAuthentication 库与 UseBasicAuthentication 与 Webapi 和 OWIN 一起使用。我的控制器中的身份没有声明,并且显示未经过身份验证。
我在 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);
}
索赔按预期从会员重启中返回。
但是当我在我的控制器方法中查看它时,没有任何声明,它说没有经过身份验证..
var identity = (ClaimsPrincipal)Thread.CurrentPrincipal;
我错过了什么?