はい、データベースにアクセスできます。 Configure
メソッドで実行されるコードは、データベースコンテキストなどを含むConfigureServices
メソッドで追加されたすべてのサービスにアクセスできます。例えば
あなたは、単純なEntity Frameworkのコンテキストがある場合、:
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; }
}
}
をそして、あなたはConfigureServices
にそれを追加します。
services.AddDbContext<SimpleContext>(opt => opt.UseInMemoryDatabase());
次にあなたがミドルウェアを設定しているとき、あなたはそれにアクセスすることができます:
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)
});
そしてGetIdentity
方法LITTを書き換えます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
}
私は元のサンプルの著者です。申し訳ありません、当初は明らかでした!私はIdentityResolver
デリゲートを、独自のデータベースとの統合(上記のような)やASP.NET Core Identityへのフック付けなど、独自の機能を簡単に提供できるように作成しようとしました。もちろん、あなたは自分のコードを捨てて、何か良いことをすることは自由です。 :)
あなただけのASPNETアイデンティティにJWTを追加している場合は、代わりにdbcontextのsigninmanagerを渡すことがあります。 (userManager)) –
xcud
@xcudこれはまさに私がやろうとしているのですが、「スコープされたサービス 'Microsoft.AspNetCore.Identity.UserManager'を解決できません」というエラーが表示されます。私はここで何が欠けているのですか? –