#region S# License /****************************************************************************************** NOTICE!!! This program and source code is owned and licensed by StockSharp, LLC, www.stocksharp.com Viewing or use of this code requires your acceptance of the license agreement found at https://github.com/StockSharp/StockSharp/blob/master/LICENSE Removal of this comment is a violation of the license agreement. Project: StockSharp.Community.Community File: AuthenticationClient.cs Created: 2015, 11, 11, 2:32 PM Copyright 2010 by StockSharp, LLC *******************************************************************************************/ #endregion S# License namespace StockSharp.Community { using System; using System.Security; using System.Threading; using Ecng.Common; using StockSharp.Logging; /// /// The client for access to the StockSharp authentication service. /// public class AuthenticationClient : BaseServiceClient { static AuthenticationClient() { _instance = new Lazy(() => new AuthenticationClient()); } private Timer _pingTimer; private readonly SyncObject _pingSync = new SyncObject(); /// /// Initializes a new instance of the . /// public AuthenticationClient() : this(new Uri("https://stocksharp.com/services/authenticationservice.svc")) { } /// /// Initializes a new instance of the . /// /// Service address. public AuthenticationClient(Uri address) : base(address, "authentication") { Credentials = new ServerCredentials(); } private static readonly Lazy _instance; /// /// The common authorization client for the whole application. /// public static AuthenticationClient Instance => _instance.Value; /// /// Information about the login and password for access to the StockSharp. /// public ServerCredentials Credentials { get; } /// /// Product. /// public Products? Product { get; set; } /// /// Product. /// public Version Version { get; set; } /// /// Has the client successfully authenticated. /// public bool IsLoggedIn => NullableSessionId != null; /// /// Session ID. /// public Guid SessionId { get { if (!IsLoggedIn) Login(); return NullableSessionId.Value; } } /// /// To get the if the user was authorized. /// public Guid? NullableSessionId { get; private set; } /// /// The user identifier for . /// public long UserId { get; private set; } /// /// To log in. /// public void Login() { Login(Product, Credentials.Email, Credentials.Password); } /// /// To log in. /// /// Product. /// Login. /// Password. public void Login(Products? product, string login, SecureString password) { if (login.IsEmpty()) throw new ArgumentNullException(nameof(login)); if (password.IsEmpty()) throw new ArgumentNullException(nameof(password)); Guid sessionId; if (product == null) { sessionId = Invoke(f => f.Login(login, password.To())); sessionId.ToErrorCode().ThrowIfError(); NullableSessionId = sessionId; UserId = Invoke(f => f.GetId(sessionId)); } else { var tuple = Invoke(f => Version == null ? f.Login2(product.Value, login, password.To()) : f.Login3(product.Value, Version.To(), login, password.To())); tuple.Item1.ToErrorCode().ThrowIfError(); NullableSessionId = tuple.Item1; UserId = tuple.Item2; } if (product != null) { _pingTimer = ThreadingHelper.Timer(() => { try { var session = NullableSessionId; if (session == null) return; lock (_pingSync) { Invoke(f => f.Ping(session.Value)); } } catch (Exception ex) { ex.LogError(); } }).Interval(TimeSpan.FromMinutes(10)); } } /// /// Logout. /// public void Logout() { Invoke(f => f.Logout(SessionId)); NullableSessionId = null; UserId = 0; _pingTimer.Dispose(); } /// /// Get a user id. /// /// Session ID. /// User id. public long GetId(Guid sessionId) { return Invoke(f => f.GetId(sessionId)); } /// /// Release resources. /// protected override void DisposeManaged() { if (IsLoggedIn) Logout(); base.DisposeManaged(); } } }