自分のカスタムデータベースでIdentityServer4を使用します。私は管理者と学生のために別々のテーブルを用意しています。IdentityServer4で既存のDBを使用する方法
既存のDBで動作するようにIdentityServer EFデータベースを設定する方法を知りたいですか?
ご協力いただければ幸いです。
ありがとうございました。
自分のカスタムデータベースでIdentityServer4を使用します。私は管理者と学生のために別々のテーブルを用意しています。IdentityServer4で既存のDBを使用する方法
既存のDBで動作するようにIdentityServer EFデータベースを設定する方法を知りたいですか?
ご協力いただければ幸いです。
ありがとうございました。
私はIdentityServer4 Quick Start sample 7_JavaScriptClientから始めました。
私は自分のデータソースに接続し、ユーザー名とパスワードでユーザーを検証し、そのユーザーオブジェクトを返すことのできるUserManagerクラスを作成しました。
public class UserManager
{
private SecurityContext _context;
public UserManager(SecurityContext context)
{
_context = context;
}
public Task<User> Find(string username, string password)
{
...Logic to query your custom user table(s)...
}
public Task<List<Claim>> GetClaimsAsync(User user)
{
var claims = new List<Claim>();
//custom database call here to where you store your claims.
var myClaims = ...Your Database call here...
var claimGroupName = "SomeCustomName";
if (security != null && security.Count() > 0)
{
foreach (var claim in security)
{
//Add the value from the field Security_Id from the database to the claim group "SomeCustomName".
claims.Add(new Claim(claimGroupName , claim.SECURITY_ID));
}
}
return Task.FromResult(claims);
}
}
ユーザーオブジェクト
public class User
{
public string FIRST_NAME { get; set; }
public string LAST_NAME { get; set; }
public string EMAIL { get; set; }
...Other attributes/properties...
}
第二に、私は、試料中のAccountControllerからのUserManagerオブジェクトを使用していました。
private readonly UserManager _userManager;
public AccountController(
IIdentityServerInteractionService interaction,
IClientStore clientStore,
IHttpContextAccessor httpContextAccessor,
UserManager userManager
)
{
_userManager = userManager;
}
AccountController.Login()HTTP Postメソッドの3番目に、ユーザーを返すためにUserManager.Find(username、password)を呼び出しました。
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginInputModel model)
{
// validate username/password
var user = await _userManager.Find(model.Username, model.Password);
//sign the user in with a subject[user_id] and name[web_id]
await HttpContext.Authentication.SignInAsync(user.USER_ID, user.WEB_ID, props);
}
第4回私はIProfileServiceを実装しました。 [私は、資源としてこのarticleを使用。]
public class ProfileService : IProfileService
{
UserManager _myUserManager;
private readonly ILogger<ProfileService> _logger;
public ProfileService(ILogger<ProfileService> logger)
{
_logger = logger;
_myUserManager = new UserManager(new SecurityContext());
}
//Called by IdentityServer Middleware.
public async Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var sub = context.Subject.FindFirst("sub")?.Value;
if (sub != null)
{
var user = await _myUserManager.FindByNameAsync(sub);
//Call custom function to get the claims from the custom database.
var cp = await getClaims(user);
var claims = cp.Claims;
...Optionaly remove any claims that don't need to be sent...
context.IssuedClaims = claims.ToList();
}
}
//Called by IdentityServer Middleware.
public async Task IsActiveAsync(IsActiveContext context)
{
var sub = context.Subject.GetSubjectId();
var user = await _myUserManager.FindByNameAsync(sub);
context.IsActive = user != null;
return;
}
//Custom function to get claims from database via the UserManager.GetClaimsAsync() method.
private async Task<ClaimsPrincipal> getClaims(User user)
{
var id = new ClaimsIdentity();
//set any standard claims
id.AddClaim(new Claim(JwtClaimTypes.PreferredUserName, user.USER_ID));
//get custom claims from database or other source.
id.AddClaims(await _myUserManager.GetClaimsAsync(user));
return new ClaimsPrincipal(id);
}
}
を最後にStartup.csセットアップで依存性注入のための任意のオブジェクトを。
public void ConfigureServices(IServiceCollection services)
{
builder.Services.AddTransient<IProfileService, ProfileService>();
//This is the DbContext to our Database where the users and their claims are stored.
services.AddTransient<SecurityContext>();
services.AddTransient<UserManager>();
}
同様のものがquestion and anserである。
また、IResourceOwnerPasswordValidatorインターフェイスとOAuth 2.0リソースオーナーのパスワード認証情報(パスワードとも呼ばれます)を認証するための実装も参照しています。 GrantTypes.ResourceOwnerPasswordAndClientCredentialsまたはGrantTypes.ResourceOwnerPasswordと共に使用されます。
詳細返信ありがとう:-) AngularJSを使用してサインアップ画面またはログイン画面を構築し、ユーザーをIdentityServerにリダイレクトしない方法を知りたかったのですが、どんな助力も高く評価されます。 ありがとうございます。 – OBA
@OBAこれは別の質問のようです。そのサイトに投稿することをお勧めします。ユーザーをIdentityServerにリダイレクトしたくないと言うと、アプリケーションがログイン画面を「所有する」ログイン画面の場合だけです。 – aaronR
@OBAサンプルプロジェクトの場合は[こちら](https://damienbod.com/2016/10/01/identityserver4-webapi-and-angular2-in-a-single-asp-net-core-project/) Angular2。さまざまなタイプのサインインプロセスを可能にするIdentityServer4フローをよりよく理解したいことがあります。 – aaronR