Home > Software engineering >  How can I run an application with just home page without logged in user?
How can I run an application with just home page without logged in user?

Time:10-10

Whenever I run my application on VS 2019 it automatically logged in with the existing name & Id without showing the register and login page. What should I do with it to display the homepage with register and Login buttons? I'm attaching SC of my startup, view and Controller files.

This is my startup.cs file

-- Below is View file--


@inject Microsoft.AspNetCore.Identity.SignInManager<AppointmentScheduler.Models.ApplicationUser> signInManager


@if (signInManager.IsSignedIn(User))
{
    <form method="post" id="logoutForm" asp-action="Logout" asp-controller="Account">
        <ul class="nav navbar text-white">
            <li>
                Hello, @User.Identity.Name!
            </li>
            <li class="offset-1">
                <a href="javascript:docuement.getElementbyId('logoutForm').submit()"> LogOut </a>
            </li>
        </ul>



    </form>
   

}
else
{
    <ul class="nav navbar offset-1">
        @*<li> @Html.ActionLink("SignUp", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
        <li> @Html.ActionLink("SignIn", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>*@

        <li class="nav nav-item">
            <a class="nav-link text-white" id="registerLink" asp-action="Register" asp-controller="Account">Register</a>
        </li>
        <li class="nav-item">
            <a class="nav nav-link text-white" id="loginLink" asp-action="Login" asp-controller="Account">SignIn</a>  
        </li>
    </ul>
}

CodePudding user response:

Whenever I run my application on VS 2019 it automatically logged in with the existing name & Id without showing the register and login page.

When the user is signed in,it will show the content in @if (signInManager.IsSignedIn(User)){},You can try to put the register and login code outside else,so that whenever user is signed or not,you can see the register and login buttons.

@inject Microsoft.AspNetCore.Identity.SignInManager<AppointmentScheduler.Models.ApplicationUser> signInManager

<ul class="nav navbar offset-1">
    @*<li> @Html.ActionLink("SignUp", "Register", "Account", routeValues: null, htmlAttributes: new { id = "registerLink" })</li>
    <li> @Html.ActionLink("SignIn", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" })</li>*@

    <li class="nav nav-item">
        <a class="nav-link text-white" id="registerLink" asp-action="Register" asp-controller="Account">Register</a>
    </li>
    <li class="nav-item">
        <a class="nav nav-link text-white" id="loginLink" asp-action="Login" asp-controller="Account">SignIn</a>  
    </li>
</ul>
@if (signInManager.IsSignedIn(User))
{
    <form method="post" id="logoutForm" asp-action="Logout" asp-controller="Account">
        <ul class="nav navbar text-white">
            <li>
                Hello, @User.Identity.Name!
            </li>
            <li class="offset-1">
                <a href="javascript:docuement.getElementbyId('logoutForm').submit()"> LogOut </a>
            </li>
        </ul>



    </form>
   

}

CodePudding user response:

You can use the AllowAnonymous attribute to allow access by non-authenticated users to individual actions. For example:

 [Authorize]
 public class HomeController : Controller
{
       [AllowAnonymous]
       public ActionResult Index
      {
       }

      public ActionResult Logout()
     {
      }
 }

This would allow only authenticated users to the homeController, except for the Index action, which is accessible by everyone, regardless of their authenticated or unauthenticated / anonymous status.

  • Related