Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DispatchGlobal/Dispatch will 400 error when execute HttpContext.SignInAsync before. #80

Open
hanxiao123 opened this issue Aug 26, 2024 · 5 comments

Comments

@hanxiao123
Copy link

I just need update userinfo header when user logoin success,but when I execute HttpContext.SignInAsync and DispatchGlobal will 400 error ,In other cases DispatchGlobal is normal.
var claims = new List
{
new Claim(ClaimTypes.Name, "111"),
new Claim(ClaimTypes.Role, "User")
};

var claimsIdentity = new ClaimsIdentity(claims, AppConst.AuthenticationScheme);
await HttpContext.SignInAsync(AppConst.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity));

DispatchGlobal(new UserEvent());

DispatchGlobal will receive 400 error.

@kjeske
Copy link
Contributor

kjeske commented Aug 28, 2024

Hi! Unfortunately I can't reproduce it. In the test project I called SignInAsync, then DispatchGlobal, and the response code was 200 OK. I would need some minimal source code package with this error to investigate further.

@hanxiao123
Copy link
Author

hanxiao123 commented Aug 30, 2024

the first time to click button,the event is 400 error:
image
but,the second time to click button is ok without doing anything:
image

@hanxiao123
Copy link
Author

hanxiao123 commented Aug 30, 2024

hydro-test-dispatch.zip
Above is the minimal source code package.

@hanxiao123
Copy link
Author

hanxiao123 commented Aug 30, 2024

DispatchGlobal is 200 OK, but the event is 400,if delete "Dispatch",it will be ok.like this
image
My intention was to refresh the header after sending the event to display the user information.

@kjeske
Copy link
Contributor

kjeske commented Sep 15, 2024

The problem is with anti-forgery token. After signing in, it changes, but Hydro still has the previous one. Then we try to trigger an event with obsolete token. Usually when users refresh the page after login, this issues doesn't occur, because the tokens are retrieved again on page reload.

Hydro already has a mechanism to auto-refresh the token, but it happens when server returns first 400 status code. So it first fails and then it gets refreshed.

For now, a dirty workaround would be to call your event twice:

DispatchGlobal(new UserEvent()); // fail and refresh token
DispatchGlobal(new UserEvent()); // will work

I will address this issue, so this hack is not needed :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants