I'm new to aspnet core 2.0 and what I would like to accomplish is to redirect a user who has not completed their profile to a page that they can do so. I'm using the default template with user authentication for Identity server.
I have tried using middleware but the page that i redirect to comes out blank. Is this the best approach or can someone help make it work. here is my middleware.
public class FarmProfileMiddleware
{
private readonly RequestDelegate next;
public FarmProfileMiddleware(RequestDelegate next)
{
this.next = next;
}
public async Task Invoke(HttpContext context, UserManager<ApplicationUser> userManager)
{
var user = await userManager.GetUserAsync(context.User);
if (user != null)
{
if (!user.EmailConfirmed && !context.Request.Path.ToString().Contains("profile"))
{
var url = context.Request.PathBase + "/Users/Profile";
context.Response.Redirect(url);
}
}
else
{
await next(context);
}
}
}
Thanks in advance.
Just looking at the current logic in your code I notice that if the user is not null and has its profile completed will short circuit the call. You need to add in that logic.
Try the following
var user = await userManager.GetUserAsync(context.User);
if (user != null && !user.EmailConfirmed && !context.Request.Path.ToString().Contains("profile")) {
var url = context.Request.PathBase + "/Users/Profile";
context.Response.Redirect(url);
} else {
await next(context);
}