Staram się ogarnąć Identity tworząc projekt typu Razor w asp.net core. Chciałem wykorzystać "klasyczne" konta (Individual) do logowania się do aplikacji.
Jest do tego dokumentacja.
https://docs.microsoft.com/en-us/aspnet/core/security/authentication/add-user-data?view=aspnetcore-2.1&tabs=visual-studio
Ale tak średnio to działa... To znaczy, nie działa. Przy próbie dodania własnych pól tego szkieletu (choćby imię i nazwisko) wszystko się rozjeżdża.
Postępuję zgodnie z instrukcją i nagle w katalogu Areas/Identity tworzy mi się jakiś alternatywny dbContext. Nie mam pojęcia jak go podłączyć pod startup, żeby to wszystko działało.
Jeśli macie jakiś link do normalnego tutorialu to wrzućcie proszę.
Może zacznij od tego: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/identity?view=aspnetcore-2.1&tabs=visual-studio
Albo pokaż co już masz.. Na pewno robisz coś źle.
Ok udało się. Tutorial jest ewidentnie skopany ale.. idąc krok po kroku, uzupełniając luki w opisach i znajdując rozwiązania błędów w googlach udało mi się w końcu osiągnąć efekt. Aż boje się przejść do kolejnego kroku. W każdym razie...
Zainstalowany .net core to 2.2.100-preview2-009404, na 2.1 szło mi równie krzywo :)
Używam ms studio code,
Startujemy jeszcze raz z projektem, tym razem gołym, bez flagi -au na starcie
dotnet new razor -o WebApp1
dotnet add package Microsoft.EntityframeworkCore.Sqlite
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet aspnet-codegenerator identity -u WebApp1User -sqlite -fi "Account.Register"
Konfiguracja sturtup
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)
{
services.AddDbContext<WebApp1IdentityDbContext>(options =>
options.UseSqlite(
Configuration.GetConnectionString("WebApp1IdentityDbContextConnection")));
services.AddDefaultIdentity<WebApp1User>()
.AddEntityFrameworkStores<WebApp1IdentityDbContext>();
services.Configure<IdentityOptions>(options =>
{
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 6;
options.Password.RequiredUniqueChars = 1;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// User settings.
options.User.AllowedUserNameCharacters =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
});
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
});
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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/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.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
}
}
Teraz update bazy
dotnet ef migrations add initial
dotnet ef database update
Nie działa, chyba, że się usunie trochę tekstu z pliku IdentityHostingStartup
//services.AddDefaultIdentity<WebApp1User>()
// .AddEntityFrameworkStores<WebApp1IdentityDbContext>();
Trzeba jeszcze pamiętać o dodaniu <partial name="_LoginPartial" /> do _Layout
I jest. Można prostować.