context.Request.User es nulo en OWIN OAuthAuthorizationServerProvider
Estoy tratando de implementar OAuth usando OWIN para un punto final Web API v2 en mi intranet local. La API está alojada en IIS utilizando la autenticación de Windows incorporada. En resumen, esto es lo que quiero que suceda.
Cuando solicito mi token en / token
Extraiga WindowsPrincipal del contexto OWIN
Use el SID de WindowsPrincipal para buscar algunos roles para este usuario en una tabla SQL.
Cree una nueva ClaimsIdentity que almacene el nombre de usuario y los roles
Convierta eso en un Json Web Token (JWT) que envié bak
Cuando solicito un recurso de mi API usando mi token
Convierta el token JWT Bearer de nuevo a ClaimsIdentity
Use esa ClaimsIdentity para autorizar solicitudes al recurso por rol
De esta manera, no tengo que hacer una búsqueda en la base de datos para roles de usuario en cada solicitud. Se acaba de hornear en el JWT.
Creo que estoy configurando todo correctamente. Mi método Startup.Configuration tiene este aspecto.
public void Configuration(IAppBuilder app)
{
// token generation
// This is what drives the action when a client connects to the /token route
app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
{
// for demo purposes
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromHours(8),
AccessTokenFormat = GetMyJwtTokenFormat(),
Provider = new MyAuthorizationServerProvider()
});
//// token consumption
app.UseOAuthBearerAuthentication(
new OAuthBearerAuthenticationOptions()
{
Realm = "http://www.ccl.org",
Provider = new OAuthBearerAuthenticationProvider(),
AccessTokenFormat = GetMyJwtTokenFormat()
}
);
app.UseWebApi(WebApiConfig.Register());
}
MyAuthorizationServerProvider tiene este aspecto ...
public class MyAuthorizationServerProvider : OAuthAuthorizationServerProvider { public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { // Since I'm hosting in IIS with Windows Auth enabled // I'm expecting my WindowsPrincipal to be here, but it's null :( var windowsPrincipal = context.OwinContext.Request.User.Identity; // windowsPrincipal is null here. Why? // Call SQL to get roles for this user // create the identity with the roles var id = new ClaimsIdentity(stuff, more stuff); context.Validated(id); } }
Mi problema es ese contexto.Request.User es nulo aquí. No puedo acceder a mi WindowsPrincipal. Si creo algún otro middleware ficticio, puedo acceder a WindowsPrincipal sin problemas. ¿Por qué es nulo en este contexto? ¿Estoy haciendo algo mal?