Reputation: 1005
I have middleware used for authentication purposes and need to distinguish users by the type if they are internal or external. After this I want to redirect external users to custom error page, because they should not have access to particular section of my site.
I wanted to throw HttpResponseException
with my custom status code and handle it by linking in web config's section customErrors
with my error page. However I cannot use custom status code this way and to use one of the existing codes is bad idea in my case.
To be honest I am quite new to OWIN middleware and I am not even sure what to google. I will be most grateful for hints how to solve such situation.
Upvotes: 4
Views: 7329
Reputation: 3627
Hey take a look at this:
public void Configuration(IAppBuilder app)
{
ConfigureApp(app);
//Some other code here
if (_helper.IsNew())
{
Debug.Write("This is a new guy. Redirect him to the setup page permanently");
var ctx = new OwinContext();
ctx.Response.Redirect("/Setup");//This is how I redirect from Startip.cs
}
}
Observe the code where I am checking in IF for truthy, on true I will create a new OWIN CONTEXT and do redirect there. I can also specify the redirection status code: 301/302. But without that also the above code works just fine
Upvotes: 4
Reputation: 13182
You should be able to set response code
(303 See Other
) to the response
object in middle-ware and a location
header to where you want to redirect user.
When you do that based you you business logic you should not call next
function and user will be redirected to new location (if no other middle-ware down the stack will change the response).
Remember to register your middle-ware before web api or mvc.
You can google for web api building custom middleware
. Here is an example article you could find Writing Custom Middleware for ASP.NET.
Upvotes: 0