- UI type: Blazor server
- DB provider: EF Core
- Tiered (MVC) or Identity Server Separated (Angular): no
- Exception message and stack trace:
- Steps to reproduce the issue:How to set the project to jump directly to the login page
12 Answer(s)
-
0
hi
Have you tried using
NavigationManager
inIndex
page? -
0
Haven't tried, help provide a sample code.
Like your example website, it automatically jumps to the login page.
-
0
hi
You can give it a try. I think it will be works.
Like your example website, it automatically jumps to the login page.
The demo website is angular. You are using Blazor Server.
-
0
I am using blazor server, and I found that your example is not enough. The effect I want is that when accessing the system homepage, if there is no login, it will automatically jump to the login page. There is no need to manually click login and then jump to the login page.
-
0
You can inject the ICurrentUser service and use IsAuthenticated to check the user whatever is logged.
-
0
hi
You can give it a try. I think it will be works.
Like your example website, it automatically jumps to the login page.
The demo website is angular. You are using Blazor Server.
Hi maliming,
Where the code for Angular. I also need to do like jumping to the login page. We use angular and Identity Server Separated.
Thx
-
0
hi
You can try this method in Angular.
this.authService.navigateToLogin();
-
0
Hi Maliming,
it was good to have an example on how to do it using mvc, I have the same need and cannot find a proper example.
Thx
-
0
hi pvaz
You can call
ChallengeAsync
in yourOnGetAsync
method ofIndexModel
for MVC tiered application.public class IndexModel : MyProjectNamePageModel { public async Task OnGetAsync() { if (!CurrentUser.IsAuthenticated) { await HttpContext.ChallengeAsync("oidc"); } } public async Task OnPostLoginAsync() { await HttpContext.ChallengeAsync("oidc"); } }
-
0
Hi Maliming,
thank you for your quick reply.
Perhaps my question I was not clear. When the user is not authenticated to redirect to /Account/Login.
Using your example I get:
"An unhandled exception occurred while processing the request. InvalidOperationException: No authentication handler is registered for the scheme 'oidc'. The registered schemes are: idsrv, idsrv.external, Abp.ConfirmUser, Identity.Application, Identity.External, Identity.TwoFactorRememberMe, Identity.TwoFactorUserId, Bearer, Google, Microsoft, Twitter. Did you forget to call AddAuthentication().AddSomeAuthHandler? Microsoft.AspNetCore.Authentication.AuthenticationService.ChallengeAsync(HttpContext context, string scheme, AuthenticationProperties properties)"
Thx
-
0
hi pvaz
I think you can redirect user to any page.
if (!CurrentUser.IsAuthenticated) { //redirect to /Account/Login }
https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.controllerbase.redirect?view=aspnetcore-5.0
-
0
Hi,
solved this way using your advice:
var redirectUrl = UriHelper.BuildAbsolute(HttpContext.Request.Scheme, HttpContext.Request.Host, "/Account/Login"); await HttpContext.ChallengeAsync(new AuthenticationProperties { RedirectUri = redirectUrl });
return;`