Ecco come si esegue l'override di OwinMiddleware ... prima abbiamo creato il nostro middleware su Owin ... Penso che abbiamo avuto un problema simile a quello che hai fatto.
Prima necessità di creare una costante:
public class Constants
{
public const string OwinChallengeFlag = "X-Challenge";
}
E sovrascrivere l'OwinMiddleware
public class AuthenticationMiddleware : OwinMiddleware
{
public AuthenticationMiddleware(OwinMiddleware next) : base(next) { }
public override async Task Invoke(IOwinContext context)
{
await Next.Invoke(context);
if (context.Response.StatusCode == 400 && context.Response.Headers.ContainsKey(Constants.OwinChallengeFlag))
{
var headerValues = context.Response.Headers.GetValues(Constants.OwinChallengeFlag);
context.Response.StatusCode = Convert.ToInt16(headerValues.FirstOrDefault());
context.Response.Headers.Remove(Constants.OwinChallengeFlag);
}
}
}
Nel file startup.Auth, ci ha permesso di overrid del Invoke Owin Comandi
public void ConfigureAuth(IAppBuilder app)
....
app.Use<AuthenticationMiddleware>(); //Allows override of Invoke OWIN commands
....
}
E in ApplicationOAuthProvider, abbiamo modificato GrantResourceOwnerCredentials.
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (UserManager<IdentityUser> userManager = _userManagerFactory())
{
IdentityUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
context.Response.Headers.Add(Constants.OwinChallengeFlag, new[] { ((int)HttpStatusCode.Unauthorized).ToString() }); //Little trick to get this to throw 401, refer to AuthenticationMiddleware for more
//return;
}
....
fonte
2015-05-13 19:06:20
Ci proverò domani e ti confermerò se ha funzionato per me !! Grazie in anticipo;) –
Hey, ho provato il tuo approccio e ho riscontrato problemi. Immagino che 'STTIAuthenticationMiddleware' sia' AuthenticationMiddleware', e il mio problema principale è che il middleware personalizzato non viene mai chiamato durante la mia pipeline. Ho configurato il mio middleware usando 'app.Uso()' e 'app.Usato (System.Type)'. Cosa c'è che non va? –
Sì, nella prima parte, sono gli stessi. Dall'altra parte, hai verificato che startup.auth chiama ConfigureAuth e lo imposta? –