MVC 4 Redirect from login page if authenticated - asp.net-mvc-4

By using a default MVC 4 web application how can I redirect the user to certain page if he is logged in? What I mean when you try to access a page that is decorated with [Authorize] attribute, it will redirect you to login page and the parameter in the url is the page you tried to access. If login succeeded, you will be redirected to that specific page. Now you are authenticated and you can access that specific page. But if you go back to the login page http://page:port/Account/Login it will still display the login page. How can I redirect the user from login page if he/she is already loged in?
Should I use this in the controller GET Login action from Account Controller?
if(Request.IsAuthenticated)
{
...redirect to ...
}

You can use RedirectToAction in your controller like this:
if(Request.IsAuthenticated)
{
return RedirectToAction("Index","Home");
}

Related

Problem in Laravel Middleware Auth and Verified Routing using Laravel Fortify "Register Email Verification"

I have successfully configured all the facilities using Laravel Fortify but I have faced a little problem in middleware routing that is I know that When we try to apply email verification we use "Auth" and "Verified" middleware to achieve it. Like...
Route::get('/', function () {
return view('home');
})->middleware(['auth', 'verified']);
It works. But I can't go home page, it redirected me to the login page. When I remove Auth Middleware and try to go home page, it also redirected me to the login page. If I remove Verified Middleware and try to register, it redirected me to the home page with logged-in instead of to go Email Verify Message Page But the email was successfully sent.
How to solve this problem, when an unregistered user goes to the home page and sees Login in Navigation like E-commerce Website and besides that when a user tries to register, the email has to verify and redirect to home page.

Authorize attribute is redirecting me to Login action even If I signed in successfully

I am working on simple login signup in ASP.NET core application. I am using SignInManager class for user to get signin.
When user tries to login the following function will get executed:
When the user enters correct credentials it will redirect to index action in home controller. I have added Authorize attribute for privacy action in home controller:
My question is if I enter correct credentials it is redirecting me to index action in home controller as expected. But if I try to access privacy action in home controller the Authorize attribute is redirecting me to login action in Account controller even if I successfully logged in. What is causing the problem here?
if I try to access privacy action in home controller the Authorize attribute is redirecting me to login action in Account controller even if I successfully logged in
Please check and make sure you call app.UseAuthentication() and app.UseAuthorization() in correct order shown as below.
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
//...

How to get the previous route in blazor?

How do I get the previous route in blazor?
For example, we have many pages which required authentication. So when a user tries to access that page we navigate the user to the login page.
After success full login we want a user to redirect back to the page he requested.

Blazor WASM AAD auth always returns to homepage

I have my Blazor WASM site set up with Azure AD Authentication, and it works great. However, if I am sent to authenticate from any page that is not the homepage (for example mysite.com/counter), when the auth is successful I am redirected to the homepage (mysite.com) I assume there is some state that I can save client side of where the user was before the user was redirected for authentication but I cannot find it.
Edit: I Did some more digging and realized that if a user already has sign in before and is coming back to the site with a page link (for example: mysite.com/counter), it works no problem. However, if a user has not authenticated and it is sent to the login.microsoftonline.com by the authorize attribute of my page the redirect url that is sent is the mysite.com/authentication/login-callback instead of the mysite.com/counter
You could use RedirectToLogin component, it preserves the current URL that the user is attempting to access so that they can be returned to that page if authentication is successful.
#inject NavigationManager Navigation
#using Microsoft.AspNetCore.Components.WebAssembly.Authentication
#code {
protected override void OnInitialized()
{
Navigation.NavigateTo(
$"authentication/login?returnUrl={Uri.EscapeDataString(Navigation.Uri)}");
}
}
Reference - https://learn.microsoft.com/en-us/aspnet/core/blazor/security/webassembly/standalone-with-azure-active-directory?view=aspnetcore-5.0#redirecttologin-component

Redirect on successful Login using servicestack

I've recently decided to migrate over to using servicestack authentication. From what I can tell, to have a redirect after a successful login of an oauth provider, you add the url to the appSettings of your web.config, i.e. oauth.GoogleOpenId.RedirectUrl.
My question is, is there anyway to make this more dynamic so that if a user get's redirected to the log on page when trying to access an authorized page, say their profile page, that once they log on successfully they get routed to their profile page instead of what's configured in the web.config? Forms authentication did this by using a 'returnUrl' query parameter.
Any help would be appreciated.
The behavior of accessing a protected page, redirecting to a /login page (overridable with HtmlRedirect on AuthFeature or Authenticate attribute) and on successful login should automatically redirect to the previously attempted protected page.
To do this you want to specify the redirect url in the continue or ReturnUrl FormData POST variable or QueryString when attempting to authenticate with the /auth service.