W ten sposób nadpisujemy OwinMiddleware ... Najpierw stworzyliśmy własne oprogramowanie pośrednie na rzecz Owina ... Myślę, że mieliśmy podobny problem jak Ty.
najpierw trzeba stworzyć Constant:
public class Constants
{
public const string OwinChallengeFlag = "X-Challenge";
}
I przesłonić 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);
}
}
}
W pliku startup.Auth, że pozwolił overrid z Invoke Owin Komendy
public void ConfigureAuth(IAppBuilder app)
....
app.Use<AuthenticationMiddleware>(); //Allows override of Invoke OWIN commands
....
}
I w ApplicationOAuthProvider, zmieniliśmy 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;
}
....
Spróbuję tego jutro, a ja potwierdzę, czy to zadziałało! Z góry dziękuję;) –
Hej, próbowałem twojego podejścia i mam problemy. Domyślam się, że "STTIAuthenticationMiddleware" to 'AuthenticationMiddleware', a moim głównym problemem jest to, że niestandardowe oprogramowanie pośrednie nigdy nie jest wywoływane podczas mojego potoku. Skonfigurowałem moje oprogramowanie pośrednie za pomocą 'app.Użyj()' i 'app.Używanie (System.Type)'. Co jest nie tak? –
Tak, w pierwszej części są one takie same. Z drugiej strony, czy zweryfikowaliście, że startup.auth wywołuje ConfigureAuth i ustawia go? –