#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 Ecng.Common;
///
/// The client for access to the StockSharp authentication service.
///
public class AuthenticationClient : BaseServiceClient
{
static AuthenticationClient()
{
_instance = new Lazy(() => new AuthenticationClient());
}
///
/// Initializes a new instance of the .
///
public AuthenticationClient()
: this(new Uri("http://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; }
///
/// Has the client successfully authenticated.
///
public bool IsLoggedIn { get; private set; }
private Guid _sessionId;
///
/// Session ID.
///
public Guid SessionId
{
get
{
if (!IsLoggedIn)
Login();
return _sessionId;
}
}
///
/// To get the if the user was authorized.
///
public Guid? TryGetSession => IsLoggedIn ? SessionId : (Guid?)null;
///
/// To log in.
///
public void Login()
{
Login(Credentials.Email, Credentials.Password.To());
}
///
/// To log in.
///
/// Login.
/// Password.
public void Login(string login, string password)
{
if (login.IsEmpty())
throw new ArgumentNullException(nameof(login));
if (password.IsEmpty())
throw new ArgumentNullException(nameof(password));
var sessionId = Invoke(f => f.Login(login, password));
sessionId.ToErrorCode().ThrowIfError();
_sessionId = sessionId;
IsLoggedIn = true;
}
///
/// Logout.
///
public void Logout()
{
Invoke(f => f.Logout(SessionId));
IsLoggedIn = false;
}
///
/// 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();
}
}
}