Identity - Kican/Kasp GitHub Wiki
- 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
- for the first make your db context from
KIdentityDbContext
like:
public class AppIdentityDbContext : KIdentityDbContext<AppUser, KaspRole> {
public AppIdentityDbContext(DbContextOptions options) : base(options) {
}
}
- 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
- 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) {
}
}