Я не знаком с авторизацией, аутентификацией и файлами cookie .net core 2.1. Я пытаюсь реализовать веб-приложение, которое 1. отправляет электронное письмо пользователю с токеном.2. пользователь нажимает на ссылку, указанную в электронном письме, чтобы войти в приложение. 3. Мы создаем файл cookie / сеанс для пользователя, который действителен только до тех пор, пока открыто окно браузера.3. Атрибут authorize должен использоваться в действиях контроллера, а вошедший в систему пользователь должен быть доступен для связывания страниц вместе. 4. Отображать имя пользователя, вошедшего в систему, в представлении mvc
. Вот что у меня есть: Startup.cs
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using VVF_Entity.Models;
using Prototype.Services;
using System;
namespace Prototype
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var AppSettingsSection = Configuration.GetSection("AppSettings");
services.AddHttpContextAccessor();
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDistributedMemoryCache();
services.AddSession(options =>
{
// Set a short timeout for easy testing.
options.IdleTimeout = TimeSpan.FromSeconds(10);
options.Cookie.HttpOnly = true;
});
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie();
//.AddCookie(options =>
//{
// options.LoginPath = "/User/Login/";
//});
services.AddMvc();
services.AddSingleton<IEmailSender, AuthMessageSender>();
services.AddDbContext<VVFContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
UserController.cs
public async Task<ActionResult> Login(Guid authcode)
{
if (authcode == null)
{
return NotFound();
}
var submitter = await _context.Submitters
.FirstOrDefaultAsync(m => m.Token == authcode);
if (submitter == null)
{
return NotFound();
}
else
{
if(submitter.ModifiedDate > DateTime.Now.AddHours(-1))
{
submitter.EmailConfirmed = true;
_context.Update(submitter);
await _context.SaveChangesAsync();
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, submitter.FirstName)
};
ClaimsIdentity userIdentity = new ClaimsIdentity(claims, "login");
ClaimsPrincipal principal = new ClaimsPrincipal(userIdentity);
await HttpContext.SignInAsync(principal);
//return View(submitter);
return RedirectToAction("Index", "Vehicles");
}
else
{
return RedirectToAction("Index", "Home");
}
}
}
VehiclesController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using VVF_Entity.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace VVF_Web.Controllers
{
[Authorize]
public class VehiclesController : Controller
{
private readonly VVFContext _context;
public VehiclesController(VVFContext context)
{
_context = context;
}
// GET: Vehicles
public async Task<IActionResult> Index()
{
// TO DO: Where SubmitterId = Authenticated Submitter
var VVFContext = _context.Vehicles.Include(v => v.ExemptReason).Include(v => v.Submitter);
return View(await VVFContext.ToListAsync());
}
Я получаю 404, и вместо этого я получаю этот URL: http://localhost:5036/Account/Login?ReturnUrl=%2FVehiclesтранспортных средств / индекса, а также я не уверен, установлен ли файл cookie или есть ли у пользователя доступ к другим страницам для отображения результатов.