Middleware not invoking when expected in .net core 2.1
In my startup.cs file in my configure method I have the following:
public void Configure(IApplicationBuilder app)
{
if (HostingEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
//TODO: Create an exception handler controller...
app.UseExceptionHandler("/Home/Error");
}
// app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent()); //required to force URL to https
app.UseStaticFiles();
app.UseSession();
app.UsePartialPageRendering();
app.Use(async (context, next) =>
{
await next.Invoke();
if (context.Response.StatusCode == 401)
{
context.Response.Redirect("/user/sign-in");
}
});
app.UseMvc();
// Log the environment so we can tell if it is correct.
Logger.LogInformation($"Hosting environment: {HostingEnvironment.EnvironmentName}");
}
Then in an Authorisation filter I have some code and in some conditions I have:
var authorisationCookie = _httpContext.HttpContext.Request.Cookies.Where(t => t.Key == "aut").FirstOrDefault();
//no valid cookie
if (authorisationCookie.Key == null)
{
//Kill the pipeline so that view doesn't get displayed.
context.Result = new UnauthorizedResult(); //should be this, but trouble handling it client side.
return;
}
When this line is hit if I use google developer tools I can see that a 401 is correctly returned, but my pipeline above never gets hit. How do I need to change this so that anytime a 401 is raised from anywhere in my app, the redirect occurs?
c# asp.net-core asp.net-core-mvc
|
show 3 more comments
In my startup.cs file in my configure method I have the following:
public void Configure(IApplicationBuilder app)
{
if (HostingEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
//TODO: Create an exception handler controller...
app.UseExceptionHandler("/Home/Error");
}
// app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent()); //required to force URL to https
app.UseStaticFiles();
app.UseSession();
app.UsePartialPageRendering();
app.Use(async (context, next) =>
{
await next.Invoke();
if (context.Response.StatusCode == 401)
{
context.Response.Redirect("/user/sign-in");
}
});
app.UseMvc();
// Log the environment so we can tell if it is correct.
Logger.LogInformation($"Hosting environment: {HostingEnvironment.EnvironmentName}");
}
Then in an Authorisation filter I have some code and in some conditions I have:
var authorisationCookie = _httpContext.HttpContext.Request.Cookies.Where(t => t.Key == "aut").FirstOrDefault();
//no valid cookie
if (authorisationCookie.Key == null)
{
//Kill the pipeline so that view doesn't get displayed.
context.Result = new UnauthorizedResult(); //should be this, but trouble handling it client side.
return;
}
When this line is hit if I use google developer tools I can see that a 401 is correctly returned, but my pipeline above never gets hit. How do I need to change this so that anytime a 401 is raised from anywhere in my app, the redirect occurs?
c# asp.net-core asp.net-core-mvc
At a guess: you're calling.Use(...)
after you've called.UseAuthentication(...)
– John
Nov 21 '18 at 8:35
@John no I don't. I've embellished my question with part of my code for returning the 401
– bilpor
Nov 21 '18 at 8:38
2
Can you show more of the code from Startup's Configure method?
– John
Nov 21 '18 at 8:39
1
@John Hi John I've updated my code to show the whole method
– bilpor
Nov 21 '18 at 8:42
Can you try and reduce this down to a Minimal, Complete, and Verifiable example? I've tried a project that does something similar with anIAuthorizationFilter
and customUse(...)
and it works as expected.
– Kirk Larkin
Nov 21 '18 at 8:51
|
show 3 more comments
In my startup.cs file in my configure method I have the following:
public void Configure(IApplicationBuilder app)
{
if (HostingEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
//TODO: Create an exception handler controller...
app.UseExceptionHandler("/Home/Error");
}
// app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent()); //required to force URL to https
app.UseStaticFiles();
app.UseSession();
app.UsePartialPageRendering();
app.Use(async (context, next) =>
{
await next.Invoke();
if (context.Response.StatusCode == 401)
{
context.Response.Redirect("/user/sign-in");
}
});
app.UseMvc();
// Log the environment so we can tell if it is correct.
Logger.LogInformation($"Hosting environment: {HostingEnvironment.EnvironmentName}");
}
Then in an Authorisation filter I have some code and in some conditions I have:
var authorisationCookie = _httpContext.HttpContext.Request.Cookies.Where(t => t.Key == "aut").FirstOrDefault();
//no valid cookie
if (authorisationCookie.Key == null)
{
//Kill the pipeline so that view doesn't get displayed.
context.Result = new UnauthorizedResult(); //should be this, but trouble handling it client side.
return;
}
When this line is hit if I use google developer tools I can see that a 401 is correctly returned, but my pipeline above never gets hit. How do I need to change this so that anytime a 401 is raised from anywhere in my app, the redirect occurs?
c# asp.net-core asp.net-core-mvc
In my startup.cs file in my configure method I have the following:
public void Configure(IApplicationBuilder app)
{
if (HostingEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
//TODO: Create an exception handler controller...
app.UseExceptionHandler("/Home/Error");
}
// app.UseRewriter(new RewriteOptions().AddRedirectToHttpsPermanent()); //required to force URL to https
app.UseStaticFiles();
app.UseSession();
app.UsePartialPageRendering();
app.Use(async (context, next) =>
{
await next.Invoke();
if (context.Response.StatusCode == 401)
{
context.Response.Redirect("/user/sign-in");
}
});
app.UseMvc();
// Log the environment so we can tell if it is correct.
Logger.LogInformation($"Hosting environment: {HostingEnvironment.EnvironmentName}");
}
Then in an Authorisation filter I have some code and in some conditions I have:
var authorisationCookie = _httpContext.HttpContext.Request.Cookies.Where(t => t.Key == "aut").FirstOrDefault();
//no valid cookie
if (authorisationCookie.Key == null)
{
//Kill the pipeline so that view doesn't get displayed.
context.Result = new UnauthorizedResult(); //should be this, but trouble handling it client side.
return;
}
When this line is hit if I use google developer tools I can see that a 401 is correctly returned, but my pipeline above never gets hit. How do I need to change this so that anytime a 401 is raised from anywhere in my app, the redirect occurs?
c# asp.net-core asp.net-core-mvc
c# asp.net-core asp.net-core-mvc
edited Nov 21 '18 at 8:42
bilpor
asked Nov 21 '18 at 8:30
bilporbilpor
1,08511228
1,08511228
At a guess: you're calling.Use(...)
after you've called.UseAuthentication(...)
– John
Nov 21 '18 at 8:35
@John no I don't. I've embellished my question with part of my code for returning the 401
– bilpor
Nov 21 '18 at 8:38
2
Can you show more of the code from Startup's Configure method?
– John
Nov 21 '18 at 8:39
1
@John Hi John I've updated my code to show the whole method
– bilpor
Nov 21 '18 at 8:42
Can you try and reduce this down to a Minimal, Complete, and Verifiable example? I've tried a project that does something similar with anIAuthorizationFilter
and customUse(...)
and it works as expected.
– Kirk Larkin
Nov 21 '18 at 8:51
|
show 3 more comments
At a guess: you're calling.Use(...)
after you've called.UseAuthentication(...)
– John
Nov 21 '18 at 8:35
@John no I don't. I've embellished my question with part of my code for returning the 401
– bilpor
Nov 21 '18 at 8:38
2
Can you show more of the code from Startup's Configure method?
– John
Nov 21 '18 at 8:39
1
@John Hi John I've updated my code to show the whole method
– bilpor
Nov 21 '18 at 8:42
Can you try and reduce this down to a Minimal, Complete, and Verifiable example? I've tried a project that does something similar with anIAuthorizationFilter
and customUse(...)
and it works as expected.
– Kirk Larkin
Nov 21 '18 at 8:51
At a guess: you're calling
.Use(...)
after you've called .UseAuthentication(...)
– John
Nov 21 '18 at 8:35
At a guess: you're calling
.Use(...)
after you've called .UseAuthentication(...)
– John
Nov 21 '18 at 8:35
@John no I don't. I've embellished my question with part of my code for returning the 401
– bilpor
Nov 21 '18 at 8:38
@John no I don't. I've embellished my question with part of my code for returning the 401
– bilpor
Nov 21 '18 at 8:38
2
2
Can you show more of the code from Startup's Configure method?
– John
Nov 21 '18 at 8:39
Can you show more of the code from Startup's Configure method?
– John
Nov 21 '18 at 8:39
1
1
@John Hi John I've updated my code to show the whole method
– bilpor
Nov 21 '18 at 8:42
@John Hi John I've updated my code to show the whole method
– bilpor
Nov 21 '18 at 8:42
Can you try and reduce this down to a Minimal, Complete, and Verifiable example? I've tried a project that does something similar with an
IAuthorizationFilter
and custom Use(...)
and it works as expected.– Kirk Larkin
Nov 21 '18 at 8:51
Can you try and reduce this down to a Minimal, Complete, and Verifiable example? I've tried a project that does something similar with an
IAuthorizationFilter
and custom Use(...)
and it works as expected.– Kirk Larkin
Nov 21 '18 at 8:51
|
show 3 more comments
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407939%2fmiddleware-not-invoking-when-expected-in-net-core-2-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53407939%2fmiddleware-not-invoking-when-expected-in-net-core-2-1%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
At a guess: you're calling
.Use(...)
after you've called.UseAuthentication(...)
– John
Nov 21 '18 at 8:35
@John no I don't. I've embellished my question with part of my code for returning the 401
– bilpor
Nov 21 '18 at 8:38
2
Can you show more of the code from Startup's Configure method?
– John
Nov 21 '18 at 8:39
1
@John Hi John I've updated my code to show the whole method
– bilpor
Nov 21 '18 at 8:42
Can you try and reduce this down to a Minimal, Complete, and Verifiable example? I've tried a project that does something similar with an
IAuthorizationFilter
and customUse(...)
and it works as expected.– Kirk Larkin
Nov 21 '18 at 8:51