Identity - Kican/Kasp GitHub Wiki

setup:

  1. make this models:
  • AppUser : KaspUser
  • AppRole : KaspRole
  • RegisterModel : IUserRegisterModel
  • UserViewModel : UserPartialVmBase
  • UserEditModel : UserEditModelBase like:
public class AppUser : KaspUser {
}

public class AppUserRegisterModel : IUserRegisterModel {
	public string Email { get; set; }
	public string Password { get; set; }
}

public class UserPartialVmBase : IModel {
	public int Id { get; set; }
	public string Name { get; set; }
}

public class UserEditModelBase {
	[MaxLength(100)]
	public string Name { get; set; }
}

Create map(automapper) from other models to AppUser

  1. for the first make your db context from KIdentityDbContext like:
public class AppIdentityDbContext : KIdentityDbContext<AppUser, KaspRole> {
	public AppIdentityDbContext(DbContextOptions options) : base(options) {
	}
}
  1. add following config to your appsettings.json:
  "JWT": {
    "Key": "nmniJj9tK78A1Hy3cgr_mv1gNhF0T1ETw0YFYfNOJmMy0ota3rkqJWFFMKc_9AIoWmXw6JA9Hac2dp9KAY_S1R07TBCJJwONxSXVEvht_Fbbi9dbghcsMp_ZI3Fp",
    "Issuer": "http://127.0.0.1:5000",
    "Expire": 525600
  }
  • you must change value of the Key and its a secret key ...
  • Expire is how much this token can be valid
  1. in startup, ConfigureServices:
services.AddKasp(Configuration)
	.AddDataBase<AppIdentityDbContext>(builder => ...)
	.AddIdentity<AppUser, KaspRole, AppIdentityDbContext>()
	.AddJwt(Configuration.GetJwtConfig());

services.AddAuthentication()
	.AddJwtBearer(Configuration.GetJwtConfig());

5- in startup, Configure add

app.UseAuthentication();

6- make a controller with name AccountController from AccountApiControllerBase like:

public class AccountController : AccountApiControllerBase<AppUser, AppUserRegisterModel, UserPartialVmBase, UserEditModelBase> {
	public AccountController(IMapper mapper, IOptions<JwtConfig> config) : base(mapper, config) {
	}
}
⚠️ **GitHub.com Fallback** ⚠️