Evet, veritabanına erişebilirsiniz! Configure
yönteminde çalışan kod, veritabanı bağlamları gibi şeyler de dahil olmak üzere ConfigureServices
yönteminde eklenen tüm hizmetlere erişebilir. Basit bir varlık Framework bağlamı varsa
Örneğin,:
using Microsoft.EntityFrameworkCore;
using SimpleTokenProvider.Test.Models;
namespace SimpleTokenProvider.Test
{
public class SimpleContext : DbContext
{
public SimpleContext(DbContextOptions<SimpleContext> options)
: base(options)
{
}
public DbSet<User> Users { get; set; }
}
}
Ve ConfigureServices
bunu ekleyin:
services.AddDbContext<SimpleContext>(opt => opt.UseInMemoryDatabase());
Sonra erişebilirsiniz sen ortakatmanını ayarlarken :
var context = app.ApplicationServices.GetService<SimpleContext>();
app.UseSimpleTokenProvider(new TokenProviderOptions
{
Path = "/api/token",
Audience = "ExampleAudience",
Issuer = "ExampleIssuer",
SigningCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256),
IdentityResolver = (username, password) => GetIdentity(context, username, password)
});
Ve GetIdentity
yöntemini bir Litt yeniden le:
private Task<ClaimsIdentity> GetIdentity(SimpleContext context, string username, string password)
{
// Access the database using the context
// Here you'd need to do things like hash the password
// and do a lookup to see if the user + password hash exists
}
Orijinal örnektir. Üzgünüm başlangıçta açık değildi! IdentityResolver
delegesini, kendi veritabanınızı (yukarıdaki gibi) entegre ederek veya ASP.NET Çekirdek Kimliğine ulaştırmak gibi kendi işlevlerinizi sağlamayı kolaylaştıracak şekilde yazmaya çalıştım. Tabii ki, kodumu atmak ve daha iyi bir şey yapmakta özgürsünüz. :)
:
Bir kapsamı kullanarak GetIdentity sonra yerine GetIdentity içinde CTX uygulamayı geçti var userManager = app.ApplicationServices .GetService (typeof (UserManager@xcud Bunu yapmaya çalıştığım şey tam olarak değil, "Hatalı hizmetin çözümü açılamıyor" Microsoft.AspNetCore.Identity.UserManager', " ben burada neyi özlüyorum –