identity数据库在mysql,如何在IdentityServer4中使用现有数据库

I want to use IdentityServer4 with my custom database. I've separate tables for admin and students and both entities have separate rights.

I want to know how to configure IdentityServer EF database to work with my existing DB?

Any help would highly appreciated.

Thanks.

解决方案

First I created a UserManager class that could connect to my data source and validate a user by user name and password and return that user object.

public class UserManager

{

private SecurityContext _context;

public UserManager(SecurityContext context)

{

_context = context;

}

public Task Find(string username, string password)

{

...Logic to query your custom user table(s)...

}

public Task> GetClaimsAsync(User user)

{

var claims = new List();

//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);

}

}

User Object

public class User

{

public string FIRST_NAME { get; set; }

public string LAST_NAME { get; set; }

public string EMAIL { get; set; }

...Other attributes/properties...

}

Second I used the UserManager Object from the AccountController in the sample.

private readonly UserManager _userManager;

public AccountController(

IIdentityServerInteractionService interaction,

IClientStore clientStore,

IHttpContextAccessor httpContextAccessor,

UserManager userManager

)

{

_userManager = userManager;

}

Third in the AccountController.Login() HTTP Post method I called the UserManager.Find(username, password) to return a user.

[HttpPost]

[ValidateAntiForgeryToken]

public async Task 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);

}

Fourth I implemented the IProfileService. [I used this article as a resource.]

public class ProfileService : IProfileService

{

UserManager _myUserManager;

private readonly ILogger _logger;

public ProfileService(ILogger 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 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);

}

}

Lastly in the Startup.cs setup any Objects for Dependency Injection.

public void ConfigureServices(IServiceCollection services)

{

builder.Services.AddTransient();

//This is the DbContext to our Database where the users and their claims are stored.

services.AddTransient();

services.AddTransient();

}

Here is a similar question and anser as well.

Note that they also reference the IResourceOwnerPasswordValidator interface and implementation which is for authenticating OAuth 2.0 resource owner password credential grant (aka password). Used with GrantTypes.ResourceOwnerPasswordAndClientCredentials or GrantTypes.ResourceOwnerPassword.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值