카테고리 없음
C# api Token 생성 .NET에서 JWT 토큰 사용 03
밥돌이밥순이
2024. 8. 14. 13:21
반응형
00
1. 사용자 조회
- UserController 추가
- [Authorize] 권한 추가
2. http 405 에러
- 405 : http post 를 get 오변경
3.http 500
- 500 서버 오류
4. http 500 해결
- Program 에 아래 소스 추가
- var builder = WebApplication.CreateBuilder(args); 와 builder.Services.AddControllers(); 사이
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(options =>
{
byte[] signingKeyBytes = Encoding.UTF8
.GetBytes("1234567890123456789012345678901234567890");
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateActor = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = "https://dongsik2013.tistory.com/",// 발급자
ValidAudience = "https://dongsik2013.tistory.com/",//발급대상
IssuerSigningKey = new SymmetricSecurityKey(signingKeyBytes)
};
});
builder.Services.AddAuthorization();
5. Authoriaztion 추가
- Authoriaztion에 token 추가
6. token 적용 확인
- Body 선택
- "호출성공!" 매시지 표시
7.token으로 권한 처리
- 권한 부여 할대 일반 사용자 ,관리자 구분
- [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme, Roles = "Admin")]
- Claim(ClaimTypes.Role,"Admin") 생성 시 권한 그룹으로 Admin으로 지정
반응형