C
C#2y ago
acookook

Display the one layout based if the user is logged ( a dummy login system) in or the other if not. (

So I would like to display one layout with multiple pages (and remove Index.cshtml) and the other if the user is not logged in. I tried many ways, but I simply cannot figure out how to do that.Program.cs
builder.Services.AddRazorPages();
// Cookie settings.
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options => {options.LoginPath = "/Login"; options.AccessDeniedPath="/Login";});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()){app.UseExceptionHandler("/Error");app.UseHsts();}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy(new CookiePolicyOptions() { });
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endPoints => { endPoints.MapRazorPages();endPoints.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
});
builder.Services.AddRazorPages();
// Cookie settings.
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options => {options.LoginPath = "/Login"; options.AccessDeniedPath="/Login";});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()){app.UseExceptionHandler("/Error");app.UseHsts();}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy(new CookiePolicyOptions() { });
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endPoints => { endPoints.MapRazorPages();endPoints.MapControllerRoute(
name: "default",
pattern: "{controller=Login}/{action=Index}/{id?}");
});
The Login.cshtml on the Layout 2
@page
@model mOrders.Pages._LoginModel @{ Layout = "_LoginLayout";}<!--Login HTML-->
@page
@model mOrders.Pages._LoginModel @{ Layout = "_LoginLayout";}<!--Login HTML-->
The Viewstart.cshtml
@{
Layout = "_Layout";
}
@{
Layout = "_Layout";
}
The index.csthml on the Layout 1 (This should be removed)
@page "/"
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">Generic ASP text</div>
@page "/"
@model IndexModel
@{
ViewData["Title"] = "Home page";
}
<div class="text-center">Generic ASP text</div>
Another cshtml in the Layout 1 (Requires Login)
@page "/MakeOrders" @model PartsModel @{ViewData["Title"] = "Make Orders";}
<!--Logged in user can see this-->
@page "/MakeOrders" @model PartsModel @{ViewData["Title"] = "Make Orders";}
<!--Logged in user can see this-->
The Login function in the in login controller is the following:
public IActionResult Login()
{
var claimsIdentity = new ClaimsIdentity(new List<Claim>{...}, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties {};
Task.WaitAll(HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentiity,....)
return LocalRedirect("/MakeOrders");
}
public IActionResult Login()
{
var claimsIdentity = new ClaimsIdentity(new List<Claim>{...}, CookieAuthenticationDefaults.AuthenticationScheme);
var authProperties = new AuthenticationProperties {};
Task.WaitAll(HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentiity,....)
return LocalRedirect("/MakeOrders");
}
6 Replies
Angius
Angius2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
acookook
acookookOP2y ago
ok. thanks Ok, I now formatted the code to be more compliant to the rules The errors I am currently getting are: A circular layout reference was detected when rendering '/Pages/Shared/_LoginLayout.cshtml'. The layout page '/Pages/Shared/_LoginLayout.cshtml' has already been rendered, page crashed and issues related to the absence of the @RenderBody tag or it just straight up crashes.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
acookook
acookookOP2y ago
Unfurtunately,no. I found a way to use one layout, but I still would like to use more layouts.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity. Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server