This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[HttpPost("logout")] | |
[Authorize] | |
public ActionResult Logout() | |
{ | |
var userName = User.Identity.Name; | |
_jwtAuthManager.RemoveRefreshTokenByUserName(userName); // can be more specific to ip, user agent, device name, etc. | |
_logger.LogInformation($"User [{userName}] logged out the system."); | |
return Ok(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ApiController] | |
[Authorize] | |
[Route("api/[controller]")] | |
public class AccountController : ControllerBase | |
{ | |
private readonly ILogger<AccountController> _logger; | |
private readonly IUserService _userService; | |
private readonly IJwtAuthManager _jwtAuthManager; | |
public AccountController(ILogger<AccountController> logger, IUserService userService, IJwtAuthManager jwtAuthManager) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JwtAuthManager : IJwtAuthManager | |
{ | |
public IImmutableDictionary<string, RefreshToken> UsersRefreshTokensReadOnlyDictionary => _usersRefreshTokens.ToImmutableDictionary(); | |
private readonly ConcurrentDictionary<string, RefreshToken> _usersRefreshTokens; // can store in a database or a distributed cache | |
private readonly JwtTokenConfig _jwtTokenConfig; | |
private readonly byte[] _secret; | |
public JwtAuthManager(JwtTokenConfig jwtTokenConfig) | |
{ | |
_jwtTokenConfig = jwtTokenConfig; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | |
{ | |
// ... | |
app.UseRouting(); | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapControllers(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
var jwtTokenConfig = Configuration.GetSection("jwtTokenConfig").Get<JwtTokenConfig>(); | |
services.AddSingleton(jwtTokenConfig); | |
services.AddAuthentication(x => | |
{ | |
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
}).AddJwtBearer(x => | |
{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JwtTokenConfig | |
{ | |
public string Secret { get; set; } | |
public string Issuer { get; set; } | |
public string Audience { get; set; } | |
public int AccessTokenExpiration { get; set; } | |
public int RefreshTokenExpiration { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddSwaggerGen(c => | |
{ | |
// configure SwaggerDoc and others | |
// add JWT Authentication | |
var securityScheme = new OpenApiSecurityScheme | |
{ | |
Name = "JWT Authentication", | |
Description = "Enter JWT Bearer token **_only_**", | |
In = ParameterLocation.Header, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM scratch | |
COPY hello / | |
CMD ["/hello"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl http://timelessname.com/elfbin/helloworld.tar.gz --output helloworld.tar.gz | |
mkdir helloworld | |
tar -xvf helloworld.tar.gz -C ./helloworld |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker image ls | |
REPOSITORY TAG IMAGE ID CREATED SIZE | |
busybox latest 1c35c4412082 3 weeks ago 1.22MB | |
alpine latest a24bb4013296 4 weeks ago 5.57MB | |
nginx alpine 89ec9da68213 2 months ago 19.9MB |