public string BuildJwtToken(IConfiguration config, User user, out DateTime expTime) { var jwtConfig = config.GetSection("JwtToken"); string key = DataProtect.TryUnProtect(jwtConfig.GetValue("Key", AuthOptions.KEY)); string issuer = jwtConfig.GetValue("Issuer", AuthOptions.ISSUER); string audience = jwtConfig.GetValue("Audience", AuthOptions.AUDIENCE); int lifetime = jwtConfig.GetValue("Lifetime", AuthOptions.LIFETIME); var now = DateTime.UtcNow; expTime = now.Add(TimeSpan.FromMinutes(lifetime)); var claims = new List <Claim> { new Claim(ClaimsIdentity.DefaultNameClaimType, user.UserID.ToString()), new Claim(ClaimsIdentity.DefaultRoleClaimType, user.Role) }; var jwt = new JwtSecurityToken( issuer: issuer, audience: audience, notBefore: now, claims: claims, expires: expTime, signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256)); return(new JwtSecurityTokenHandler().WriteToken(jwt)); }
private void ConfigureServices(IServiceCollection services) { services.AddSingleton <IConsole, PxConsole>(); Pulxer.BL.ConfigureServices(services, _config, DataProtect.TryUnProtect(_config.GetConnectionString("Pulxer")), DataProtect.TryUnProtect(_config.GetConnectionString("Leech"))); }
public void Encrypt_null_argNullExp() { Assert.ThrowsException <ArgumentNullException>(() => { DataProtect.Encrypt(null, null, null); }); }
public void ConfigureServices(IServiceCollection services) { var jwtConfig = _config.GetSection("JwtToken"); string key = DataProtect.TryUnProtect(jwtConfig.GetValue("Key", AuthOptions.KEY)); services.AddAuthentication(opt => { opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(opt => { //opt.RequireHttpsMetadata = _environment.IsProduction(); opt.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = jwtConfig.GetValue("Issuer", AuthOptions.ISSUER), ValidateAudience = true, ValidAudience = jwtConfig.GetValue("Audience", AuthOptions.AUDIENCE), ValidateLifetime = true, ClockSkew = TimeSpan.Zero, IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(key), ValidateIssuerSigningKey = true }; }); services.AddControllers(); string pulxerConnectionString = DataProtect.TryUnProtect(_config.GetConnectionString("Pulxer")); string leechConnectionString = DataProtect.TryUnProtect(_config.GetConnectionString("Leech")); Pulxer.BL.ConfigureServices(services, _config, pulxerConnectionString, leechConnectionString); }
public void Decrypt_null_argNullExp() { Assert.ThrowsException <ArgumentNullException>(() => { string server; string login; string password; DataProtect.Decrypt(null, out server, out login, out password); // null на входе }); }
public LpClientApp(DataProtect dataProtect, IInstrumTable instrumTable, IAccountTable accountTable, IStopOrderTable stopOrderTable, IOrderTable orderTable, ITradeTable tradeTable, ICashTable positionTable, IHoldingTable holdingTable, ITickDispatcher tickDisp, ILogger logger) { _dataProtect = dataProtect; _socket = new LpClientSocket(); _core = new LpCore(_socket, false); // клиент _pipeFactory = new LpAppFactory(_core, instrumTable, accountTable, stopOrderTable, orderTable, tradeTable, positionTable, holdingTable, tickDisp); _sysPipe = new SystemLp(_pipeFactory, _core); _logger = logger; }
public void EncryptDecrypt_empty_empty() { string server; string login; string password; string encData = DataProtect.Encrypt("", "", ""); bool isSuccess = DataProtect.Decrypt(encData, out server, out login, out password); Assert.IsTrue(isSuccess); Assert.AreEqual("", server); Assert.AreEqual("", login); Assert.AreEqual("", password); }
public void EncryptDecrypt_localMachineScope_correctDecrypt() { string server = "server.com"; string login = "******"; string password = "******"; string encData = DataProtect.Encrypt(server, login, password, true); string server1; string login1; string password1; bool isSuccess = DataProtect.Decrypt(encData, out server1, out login1, out password1); Assert.IsTrue(isSuccess); Assert.AreEqual(server, server1); Assert.AreEqual(login, login1); Assert.AreEqual(password, password1); }
public void EncryptDecrypt_correctData_correctDecrypt() { string server = "server.com"; string login = "******"; string password = "******"; string encData = DataProtect.Encrypt(server, login, password); string server1; string login1; string password1; bool isSuccess = DataProtect.Decrypt(encData, out server1, out login1, out password1); Assert.IsTrue(isSuccess); Assert.AreEqual(server, server1); Assert.AreEqual(login, login1); Assert.AreEqual(password, password1); }
public async Task Token() { var username = Request.Form["username"]; var password = Request.Form["password"]; var identity = GetIdentity(username, password); if (identity == null) { Response.StatusCode = 400; await Response.WriteAsync("Invalid login or password."); return; } var jwtConfig = _config.GetSection("JwtToken"); string key = DataProtect.TryUnProtect(jwtConfig.GetValue("Key", AuthOptions.KEY)); string issuer = jwtConfig.GetValue("Issuer", AuthOptions.ISSUER); string audience = jwtConfig.GetValue("Audience", AuthOptions.AUDIENCE); int lifetime = jwtConfig.GetValue("Lifetime", AuthOptions.LIFETIME); var now = DateTime.UtcNow; var jwt = new JwtSecurityToken( issuer: issuer, audience: audience, notBefore: now, claims: identity.Claims, expires: now.Add(TimeSpan.FromMinutes(lifetime)), signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256)); var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt); var response = new { access_token = encodedJwt, username = identity.Name }; Response.ContentType = "application/json"; await Response.WriteAsync(JsonConvert.SerializeObject(response, new JsonSerializerSettings { Formatting = Formatting.Indented })); }
public void Decrypt_incorrectData_false() { string server; string login; string password; bool isSuccess; isSuccess = DataProtect.Decrypt("", out server, out login, out password); // пустая строка на входе Assert.AreEqual(false, isSuccess); Assert.AreEqual("", server); Assert.AreEqual("", login); Assert.AreEqual("", password); isSuccess = DataProtect.Decrypt("привет", out server, out login, out password); // не base64 на входе Assert.AreEqual(false, isSuccess); Assert.AreEqual("", server); Assert.AreEqual("", login); Assert.AreEqual("", password); }
private void FileProtect(List <string> args) { if (args.Count < 2) { _console.WriteError("Неверное число аргументов."); return; } bool isLocalMachile = args[1].ToLower().StartsWith('m'); try { string resPath = DataProtect.FileProtect(args[0], ".protected", isLocalMachile); _console.WriteLine("Сформирован файл:" + resPath); } catch (Exception ex) { _console.WriteError(ex.ToString()); } }
///... public static ArrivedDetails CreateFromEncryptedKey(string encrypted) { return DataProtect.deserializeXML(DataProtect.DecryptData(encrypted)); }