我们的起始页面应该是Views / Home / index.cshtml,但是由于某种原因,它始终默认为登录页面。一切看起来都像是正确的设置。
我无需登录即可直接导航至Insert into Tabella2 (Nome) VALUES @Nome
,当直接进入首页https://localhost/Home
Startup.cs:
https://localhost
HomeController:
public class Startup
{
public static IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
//Van - 9-29-2019: Cookie policy example: https://quixel.com/cookiepolicy
options.CheckConsentNeeded = context => false;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
});
//services.AddAuthentication(options =>
//{
// options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
// options.DefaultForbidScheme = CookieAuthenticationDefaults.AuthenticationScheme;
// options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
// options.DefaultSignOutScheme = CookieAuthenticationDefaults.AuthenticationScheme;
// options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
//});
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.SignIn.RequireConfirmedEmail = false;
options.Password.RequiredLength = 10;
options.Password.RequireLowercase = true;
options.Password.RequireUppercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireDigit = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddScoped<UserManager<ApplicationUser>>();
services.AddMemoryCache();
services.AddDistributedMemoryCache();
// For session variables, if used. Default IdleTimeout if not accessed = 20 minutes
services.AddSession(options =>
{
options.Cookie.Name = ".H.Session";
//options.IdleTimeout = TimeSpan.FromMinutes(20);
//Van - 9-26-2019: Making this essential. The session will hold the evnironment ID if the user has an environment running.
options.Cookie.IsEssential = true; // GDPR related
});
// Microsoft devs discuss expiration settings: https://github.com/aspnet/Identity/issues/1612
services.Configure<IdentityOptions>(options =>
{
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(10);
options.Lockout.MaxFailedAccessAttempts = 3;
options.Lockout.AllowedForNewUsers = true;
});
//Van - 9-26-2019: Added because in asp core 3.0 Synchronous operations are disallowed by default
//TODO: The line "data = await streamReader.ReadToEndAsync();" in TextPlainInputFormatter.cs is the problem. Should work though, its async. using this temp. workaround
services.Configure<IISServerOptions>(options =>
{
options.AllowSynchronousIO = true;
});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
//TODO: the timer below works, but does not automatically redirect, it redirects when you navigate to another page. Find a way to refresh on cookie timeout
//This also only works if pages are being changed. Not useful for range idleness
//options.ExpireTimeSpan = TimeSpan.FromMinutes(2);
//options.ExpireTimeSpan = TimeSpan.FromSeconds(5);
options.SlidingExpiration = true;
//Van - 9-20-2019: Because RedirectToLogin is being used, is this necessary? Using redirectUri in RedirectToLogin instead.
//options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
//Located in CookieEvents.cs
options.EventsType = typeof(CookieEvents);
// options.Events.OnRedirectToLogin = delegateclass. to handle whether to stopenvironment and really logout
// - what if guac/range activity is ongoing, bad UX to logout and stopenvironment. CookieAuthenticationEvents delegate class
// - http://codereform.com/blog/post/asp-net-core-2-0-authentication-with-local-logins-responding-to-backend-changes/
});
// Looks like this is policy based, not role based
services.AddMvc(options =>
{
options.InputFormatters.Add(new TextPlainInputFormatter());
options.EnableEndpointRouting = false;
})
.AddRazorPagesOptions(options =>
{
options.Conventions.AllowAnonymousToPage("/Home/Index");
options.Conventions.AddPageRoute("/Identity/Account/Login", "");
options.Conventions.AuthorizeAreaPage("Identity", "/Manage/DownloadPersonalData");
options.Conventions.AuthorizeAreaPage("Identity", "/Manage/Index");
})
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddSessionStateTempDataProvider();
//Van - 9/05/2019 - Exceptions get thrown without this line
System.Environment.SetEnvironmentVariable("AWS_ENABLE_ENDPOINT_DISCOVERY", "false");
services.AddDefaultAWSOptions(Configuration.GetAWSOptions());
services.Configure<ArenaClientOptions>(Configuration.GetSection("ArenaClient"));
services.Configure<HOptions>(Configuration.GetSection("H"));
services.Configure<BraintreeOptions>(Configuration.GetSection("Braintree"));
services.AddScoped<ISubscriptionService, SubscriptionService>();
services.AddScoped<IDashboardService, DashboardService>();
services.AddTransient<IArenaClient, ArenaClient>();
services.AddSingleton<ICacheService, CacheService>();
services.AddTransient<IEmailSenderService, EmailSenderService>();
services.Configure<EmailSenderOptions>(Configuration.GetSection("EmailSender"));
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddTransient<CookieEvents>();
services.AddSingleton<IHubHelperService, HubHelperService>();
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider provider)
{
// TODO: ASP.NET Core diagnostics?
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseExceptionHandler("/Home/Error");
//app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseAuthentication();
//app.UseHttpsRedirection();
app.UseSession();
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), "Utilities")), RequestPath = "/Utilities"
});
//app.UseCookiePolicy();
app.UseSignalR(routes =>
{
routes.MapHub<Hubs.RangeHub>("/range");
});
//Van - 9-14-2019 - using MVC with defaults should work just fine sense we are not changing the routes
// The home controller is being overridden somewhere else
app.UseMvcWithDefaultRoute();
//app.UseMvc(routes =>
//{
// routes.MapRoute(
// name: "default",
// template: "{controller=Home}/{action=Index}/{id?}");
//});
CreateRoles(provider);
}
private void CreateRoles(IServiceProvider serviceProvider)
{
var RoleManager = serviceProvider.GetRequiredService<RoleManager<IdentityRole>>();
var UserManager = serviceProvider.GetRequiredService<UserManager<ApplicationUser>>();
var roleNames = Configuration.GetSection("H:Roles").Get<string[]>();
var superuserEmail = Configuration.GetSection("H:SuperuserEmail").Get<string>();
Task<IdentityResult> roleResult;
foreach (var roleName in roleNames)
{
Task<bool> roleExist = RoleManager.RoleExistsAsync(roleName);
roleExist.Wait();
if (!roleExist.Result)
{
roleResult = RoleManager.CreateAsync(new IdentityRole(roleName));
roleResult.Wait();
}
}
// Change to appSettings value
var user = UserManager.FindByEmailAsync(superuserEmail);
user.Wait();
if (user?.Result != null)
{
var hasRole = UserManager.IsInRoleAsync(user.Result, "Superuser");
hasRole.Wait();
if (!hasRole.Result)
{
Task<IdentityResult> addToRole = UserManager.AddToRoleAsync(user.Result, "Superuser");
addToRole.Wait();
}
}
}
}
以前有人遇到过这个吗? 据我所知,这可能是由于想要在访问页面之前先对其进行身份验证而引起的,这不是我想要的主页,而是欢迎用户访问该网站。
更新1:如果我删除Login.cshtml,主页现在可以使用了,但是我当然需要登录页面。
更新2:认为这可能是身份验证问题,因此我一直在查看我的服务。ConfigureApplicationCookie选项和一切正常。我已将LoginPath注释掉,以防万一,但结果是相同的。
更新4:对于重要的事情,我在日志中看到:[AllowAnonymous]
public class HomeController : Controller
{
public HomeController()
{
}
public IActionResult Index()
{
return View();
}
public IActionResult Privacy()
{
return View();
}
/// <summary>
/// This can be access from /Home/ThrowHiddenException.
///
/// It demonstrates whether exception handling and logging in the H ASP.NET Core
/// application is working correctly. A log entry should be made.
///
/// THEX string is searchable in the logs.
/// </summary>
/// <returns></returns>
public IActionResult ThrowHiddenException()
{
throw new Exception("THEX: Test exception handling and logging.");
}
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}
}
仍然不确定代码中发生了什么。真奇怪。
答案 0 :(得分:1)
我发现了问题。登录剃须刀页面的顶部有@page "/"
,我想这种路由比启动时可能发生的任何其他路由都重要。删除“ /”解决了该问题。我将我的名称更改为“登录”。
如果其他人遇到此问题,请检查剃须刀页面的顶部。
答案 1 :(得分:0)
您的问题是由options.Conventions.AddPageRoute("/Identity/Account/Login", "");
引起的,该问题会将https://localhost
重定向到/Identity/Account/Login
。
尝试对此行options.Conventions.AddPageRoute("/Identity/Account/Login", "");
进行注释。