C
C#ā€¢2y ago
surwren

[ASP.NET] Middleware Stops Controller From Receiving Login Request

These are my buttons in /Views/Login
<form role="search" action="/Login/AddLoginCookie" method="POST">
<button class="btn btn-dark" id="test1" type="submit">Login</button>
</form>

<form role="search" action="/Login/DelLoginCookie" method="POST">
<button class="btn btn-dark" id="test2" type="submit">Logout</button>
</form>
<form role="search" action="/Login/AddLoginCookie" method="POST">
<button class="btn btn-dark" id="test1" type="submit">Login</button>
</form>

<form role="search" action="/Login/DelLoginCookie" method="POST">
<button class="btn btn-dark" id="test2" type="submit">Logout</button>
</form>
These are the buttons in /Controllers/LoginController
[HttpPost]
public IActionResult AddLoginCookie()
{

Response.Cookies.Append("sessionId", "001");
return RedirectToAction("Index", "Login");
}

//works even without HttpPost
[HttpPost]
public IActionResult DelLoginCookie()
{
Response.Cookies.Delete("sessionId");

return Redirect(Request.Headers["Referer"].ToString());
}
[HttpPost]
public IActionResult AddLoginCookie()
{

Response.Cookies.Append("sessionId", "001");
return RedirectToAction("Index", "Login");
}

//works even without HttpPost
[HttpPost]
public IActionResult DelLoginCookie()
{
Response.Cookies.Delete("sessionId");

return Redirect(Request.Headers["Referer"].ToString());
}
This is the middleware code in program.cs
app.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments("/Login/"))
{
await next(context);
return;
}

string sessionId = context.Request.Cookies["sessionId"];
if (sessionId == null)
{

context.Response.Redirect("/Login/");
}
else
{
await next(context);
}
});
app.Use(async (context, next) =>
{
if (context.Request.Path.StartsWithSegments("/Login/"))
{
await next(context);
return;
}

string sessionId = context.Request.Cookies["sessionId"];
if (sessionId == null)
{

context.Response.Redirect("/Login/");
}
else
{
await next(context);
}
});
For some reason, it looks like the middleware is preventing the user from using the controller IActionMethods in /LoginController to get a session ID. But this seems to be happening anyway even though I am checking that (context.Request.Path.StartsWithSegments("/Login/")). FWIW, even
if (context.Request.Path.StartsWithSegments("/Login/AddLoginCookie/"))
if (context.Request.Path.StartsWithSegments("/Login/AddLoginCookie/"))
will not let me through to the IActionResult. What is going wrong here?
30 Replies
Kouhai
Kouhaiā€¢2y ago
Just change if (context.Request.Path.StartsWithSegments("/Login/")) to if (context.Request.Path.StartsWithSegments("/Login"))
surwren
surwrenā€¢2y ago
lmao it actually works why doesn't /Login/ work? technically the full url is /Login/Index, so why is .StartsWithSegments() not able to get the correct path Actually a better question would be how do I troubleshoot this because nothing appears in the call stack variables
Kouhai
Kouhaiā€¢2y ago
The way StartsWithSegments defines a segment is basically /Login/ is a whole segment, so /Login/AddLoginCookie/ can't match the whole segment of /Login/ Meanwhile /Login means something can come after it because it's not terminated with / Unfortunately if you don't know the behavior of StartsWithSegments it would be a bit troublesome, your best bet would be to attach a debugger and investigate which part fails btw off topic, your pfp is really cute šŸ˜…
surwren
surwrenā€¢2y ago
You mean like x64DBG? I did put in breakpoints in MSVS but it just skipped over the initial conditional
Kouhai
Kouhaiā€¢2y ago
No no, with a managed debugger in your IDE Were you in release build?
surwren
surwrenā€¢2y ago
I think it's development, how do I change it?
Kouhai
Kouhaiā€¢2y ago
You can change it from the top menu here
surwren
surwrenā€¢2y ago
Ok, I'll try it in a few min when I'm back at the pc
Kouhai
Kouhaiā€¢2y ago
You were probably in Debug build anyways, I thought Release might've caused it to not get hit
surwren
surwrenā€¢2y ago
hmm
surwren
surwrenā€¢2y ago
yea, I am in debug
Kouhai
Kouhaiā€¢2y ago
When you placed a breakpoint what happened?
surwren
surwrenā€¢2y ago
It stops at the breakpoint but the variables of the call stack don't show any helpful info whatsoever lemme grab a screenie like
surwren
surwrenā€¢2y ago
surwren
surwrenā€¢2y ago
it shows the path then it just skips and doesn't give any indication why it evaluated false on the path
surwren
surwrenā€¢2y ago
this too
surwren
surwrenā€¢2y ago
I mean I understand the solution now, but if something similar were to occur I would have no way of troubleshooting it on my own in the future šŸ˜”
Kouhai
Kouhaiā€¢2y ago
Oh, yeah, that would be normal StartsWithSegments only returns bool so there is no way to really know why it failed with running the code. You can however run code in an interactive code thingy (I forgot what's called šŸ˜… ) and see what works with StartsWithSegments or if you're comfortable with reading the source code for it, you can find it on github
surwren
surwrenā€¢2y ago
I'm confused, what keywords should I look for for this interactive code thing? Or do you mean the source code/BCL for ASP.NET is on oogura
Kouhai
Kouhaiā€¢2y ago
Yeah, I meant ASP.NET on Github, only go that route if others fail first and you're comfortable with that šŸ˜… Lemme just open VS and show you what I meant with interactive code thingy
Kouhai
Kouhaiā€¢2y ago
Kouhai
Kouhaiā€¢2y ago
You can evaluate different expressions in Immediate Window
surwren
surwrenā€¢2y ago
Is there some setting to activate it? My immediate window is blank
surwren
surwrenā€¢2y ago
Kouhai
Kouhaiā€¢2y ago
You can just type any expression you want!
surwren
surwrenā€¢2y ago
it works! thank you I have an additional question @Kouhai Why does middleware use the async syntax/keyword?
app.Use(async (context, next) =>...
app.Use(async (context, next) =>...
Is it mandatory for middlewares to use async? I feel like I am lacking some comprehension of middleware necessary to connect the dots but idk what it is
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View
surwren
surwrenā€¢2y ago
Are middlewares always lambdas or is it this specific middleware?
Unknown User
Unknown Userā€¢2y ago
Message Not Public
Sign In & Join Server To View