public string GenerateRefreshToken(Holuser user) { var claims = new[] { new Claim(JwtRegisteredClaimNames.Sid, user.HoluserId.ToString()), new Claim(ClaimTypes.NameIdentifier, user.Account), new Claim(ClaimTypes.Name, user.Name), new Claim(ClaimTypes.Role, $"RefreshToken"), }; var token = new JwtSecurityToken ( issuer: configuration["Tokens:ValidIssuer"], audience: configuration["Tokens:ValidAudience"], claims: claims, expires: DateTime.Now.AddDays(Convert.ToDouble(configuration["Tokens:JwtRefreshExpireDays"])), //notBefore: DateTime.Now.AddMinutes(-5), signingCredentials: new SigningCredentials(new SymmetricSecurityKey (Encoding.UTF8.GetBytes(configuration["Tokens:IssuerSigningKey"])), SecurityAlgorithms.HmacSha512) ); string tokenString = new JwtSecurityTokenHandler().WriteToken(token); return(tokenString); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] HoluserDto data) { APIResult apiResult; var record = await holuserService.GetAsync(id); if (record != null) { Holuser recordTarget = mapper.Map <Holuser>(data); recordTarget.HoluserId = id; var result = mapper.Map <HoluserDto>(recordTarget); var isSuccessful = await holuserService.UpdateAsync(recordTarget); if (isSuccessful) { apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted, ErrorMessageEnum.None, payload: result); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.無法修改紀錄, payload: result); } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.沒有任何符合資料存在, payload: data); } return(Ok(apiResult)); }
public async Task <IActionResult> Post([FromBody] HoluserDto data) { APIResult apiResult; Holuser record = mapper.Map <Holuser>(data); if (record != null) { var result = mapper.Map <HoluserDto>(record); var isSuccessful = await holuserService.AddAsync(record); if (isSuccessful) { apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: result); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.無法新增紀錄, payload: result); } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.傳送過來的資料有問題, payload: data); } return(Ok(apiResult)); }
public async Task <bool> UpdateAsync(Holuser paraObject) { #region EF Core 追蹤查詢所造成的問題說明 // 若再進行搜尋該修改紀錄的時候,使用了 追蹤查詢 (也就是,沒有使用 .AsNoTracking()方法) // 將會造成快取記錄在電腦端,而這裡若要進行 // context.Entry(paraObject).State = EntityState.Modified; 呼叫與更新的時候 // 將會造成問題 // System.InvalidOperationException: The instance of entity type 'Person' cannot be tracked // because another instance with the same key value for {'PersonId'} is already being tracked. // When attaching existing entities, ensure that only one entity instance with a given key value // is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' // to see the conflicting key values. #endregion CleanTrackingHelper.Clean <Holuser>(context); Holuser item = await context.Holuser .AsNoTracking() .FirstOrDefaultAsync(x => x.HoluserId == paraObject.HoluserId); if (item == null) { return(false); } else { CleanTrackingHelper.Clean <Holuser>(context); context.Entry(paraObject).State = EntityState.Modified; await context.SaveChangesAsync(); CleanTrackingHelper.Clean <Holuser>(context); return(true); } }
public async Task <Holuser> GetAsync(int id) { Holuser item = await context.Holuser .AsNoTracking() .FirstOrDefaultAsync(x => x.HoluserId == id); return(item); }
public async Task <bool> AddAsync(Holuser paraObject) { CleanTrackingHelper.Clean <Holuser>(context); await context.Holuser .AddAsync(paraObject); await context.SaveChangesAsync(); CleanTrackingHelper.Clean <Holuser>(context); return(true); }
public async Task <(Holuser, string)> CheckUser(string account, string password) { Holuser user = await context.Holuser.AsNoTracking().FirstOrDefaultAsync(x => x.Account == account); if (user == null) { return(null, "使用者帳號不存在"); } if (user.Password != password) { return(null, "密碼不正確"); } return(user, ""); }
public async Task <bool> DeleteAsync(Holuser paraObject) { CleanTrackingHelper.Clean <Holuser>(context); Holuser item = await context.Holuser .AsNoTracking() .FirstOrDefaultAsync(x => x.HoluserId == paraObject.HoluserId); if (item == null) { return(false); } else { CleanTrackingHelper.Clean <Holuser>(context); context.Entry(paraObject).State = EntityState.Deleted; await context.SaveChangesAsync(); CleanTrackingHelper.Clean <Holuser>(context); return(true); } }
public async Task <IActionResult> RefreshToken() { APIResult apiResult; await Task.Yield(); LoginRequestDto loginRequestDTO = new LoginRequestDto() { Account = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value, }; Holuser user = await holuserService.GetAsync(Convert.ToInt32(loginRequestDTO.Account)); if (user == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status401Unauthorized, ErrorMessageEnum.沒有發現指定的該使用者資料); return(BadRequest(apiResult)); } string token = GenerateToken(user); string refreshToken = GenerateRefreshToken(user); LoginResponseDto LoginResponseDTO = new LoginResponseDto() { Account = loginRequestDTO.Account, Id = 0, Name = loginRequestDTO.Account, Token = token, TokenExpireMinutes = Convert.ToInt32(configuration["Tokens:JwtExpireMinutes"]), RefreshToken = refreshToken, RefreshTokenExpireDays = Convert.ToInt32(configuration["Tokens:JwtRefreshExpireDays"]), }; apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: LoginResponseDTO); return(Ok(apiResult)); }