0

I need to know how to redirect a user if they are signed in to my app. There are 2 parts to my problem:

  1. Set the default page to /Account/Login
  2. If the user is already signed in (Remember Me check box is checked), then redirect to BookDetails.cs controller

I have already solved problem 1 by adding the following code to Program.cs: builder.Services.AddControllersWithViews().AddRazorPagesOptions(options => { options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "")})

I know how to check if the user is currently signed in. If I wanted to check if the user is currently signed in from BookDetailsController I would include the following code in the controller file:

...
private readonly SignInManager<IdentityUser> _signInManager;

public BookDetailsController(BookClubBookDetailsContext context, SignInManager<IdentityUser> signInManager)
{
   _signInManager = signInManager;
   _context = context;
}

...

How do I redirect from Login page if the user is already logged in?

Edit:

Here is my problem described visually:

enter image description here

Meeth
  • 99
  • 1
  • 10
  • 1
    Normally, we won't set the login page as the home page, we usually set authentication on the Home page so that when app found use didn't sign in, it will automatically rediret to login page. And about your question, I think the solution should depend on which authentication method you use. I recommend you referring to [this document](https://learn.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-6.0) to learn about a sampe uing Cookie authentication in asp.net core. – Tiny Wang Apr 05 '22 at 02:01

3 Answers3

0

To redirect after login, simply you update your login method this way in AccountController

public async Task<IActionResult> Login(string returnUrl = null)
{
   //do sign in
   //if success then redirect
   if(!string.IsNullOrEmpty(returnUrl))
   {
      Redirect(returnUrl);
   }
}

if you are using page, check /account/login.cshrml.cs

public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
   returnUrl ??= Url.Content("~/");

   //signIn using SignInManager
   

   if (result.Succeeded)
   {
       //if redirected from other site 
       //return Redirect(returnUrl);

       //Same site
       return LocalRedirect(returnUrl);
   }
}
ahnirab
  • 168
  • 1
  • 11
  • But how do I redirect if I run the app, check the Remember Me check box, log in, close the app and run the app again. I remain logged in, but I'm still on the login page, instead of being redirected – Meeth Apr 04 '22 at 17:06
  • You need to configure authentication and cookie properly. Please check this - https://stackoverflow.com/questions/68074635/how-to-implement-remember-me-functionality-properly-asp-net-core . Also sign in using SignInManager.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, false); – ahnirab Apr 04 '22 at 17:19
0

Try this:

   public class HomeController : Controller
   {

      private readonly SignInManager<IdentityUser> _manager;

      public HomeController
      (
         SignInManager<IdentityUser> manager
      )
      {
         _manager = manager;
      }

      [HttpGet]
      [AllowAnonymous]
      [Route("")]
      [Route("Home")]
      [Route("Index")]
      [Route("Home/Index")]
      public IActionResult Index()
      {
         if (_manager.IsSignedIn(User))
         {
            return View("1");
         }
         else
         {
            return View("0");
         }
      }
    }

The SignInManager will help you determine if the user is authenticated.

Trystan
  • 211
  • 3
  • 5
0

If you use forms auth, you can do it in Web.config as a variant:

<system.web>
<authentication mode="Forms">
      <forms name="sessionVarName" loginUrl="~/Authorization/Login" slidingExpiration="true" path="/" defaultUrl="~/Main/Welcome" protection="None" cookieless="UseCookies"/>
    </authentication>
</system.web>

loginUrl: your login method in Controller.

defaultUrl: address, user will be redirected after successful login.

Use [Authorize] attribut in contorllers with methods for autorized users only.

Then modify your Auth controller to set valid Cookie ticket.

ElikaNaari
  • 86
  • 3