-
Notifications
You must be signed in to change notification settings - Fork 14
OperationContext In Http Protocol and Http Headers
Ali Yousefi edited this page Feb 15, 2019
·
6 revisions
Signalgo support http and https protocols too, but in signalgo every protocols architecture are same.if you setting your operation context setting like http,it will work with all protocol because http protocol operation context setting is a little more complicated (use one or two attribute).
lets start:
/// <summary>
/// this is your operation context seeting save and use it everywhere you want
/// </summary>
public class CurrentUserInfo
{
/// <summary>
/// instade of using OperationContext<CurrentUserInfo>.CurrentSetting you can use CurrentUserInfo.Current to fast access the current setting of context
/// </summary>
public static CurrentUserInfo Current
{
get
{
return OperationContext<CurrentUserInfo>.CurrentSetting;
}
}
/// <summary>
/// fast access to current user id
/// </summary>
public static int UserId
{
get
{
return Current.UserInfo.Id;
}
}
/// <summary>
/// simple databse model, save current user loggined to memory and use it everywhere witout query to database
/// </summary>
public UserInfo UserInfo { get; set; }
/// <summary>
/// you current session will save on here with HttpKey used on top of property
/// </summary>
[HttpKey]
public string Session { get; set; }
/// <summary>
/// you can set date time for expire time of session and user have to login again
/// </summary>
[HttpKey(KeyType = HttpKeyType.ExpireField)]
public DateTime ExpireDateTime { get; set; }
/// <summary>
/// a simple custome user role for admin users
/// </summary>
public bool IsAdmin { get; set; }
/// <summary>
/// a simple custome user role for normal users
/// </summary>
public bool IsNormalUser { get; set; }
}
//create Authentication service with http protocol
[ServiceContract("Authentication", ServiceType.HttpService, InstanceType.SingleInstance)]
public class AuthenticationService
{
public string Login(string userName, string password)
{
//sample for use entity framework database
using (ExpertContext context = new ExpertContext())
{
//example of query to users table
UserInfo user = context.UserInfoes.FirstOrDefault(x => x.UserName == userName x.Password == password);
//if username and password was wrong
if (user == null)
return "UsernameOrPasswordIncorrect";
//generate a guid token
string userToken = Guid.NewGuid().ToString();
//authenticate user to signalgo system
OperationContext<CurrentUserInfo>.CurrentSetting = new CurrentUserInfo()
{
UserInfo = user,
ExpireDateTime = DateTime.Now.AddDays(1),
Session = userToken,
IsAdmin = user.Id == 1,
IsNormalUser = true
};
return "success login";
}
}
/// <summary>
/// check who called this method
/// </summary>
/// <returns></returns>
public string CheckWhoAmI()
{
if (CurrentUserInfo.Current == null)
return "user is not loggined";
return CurrentUserInfo.Current.UserInfo.UserName;
}
}
call this in browser: http://localhost:8520/Authentication/Login?userName="testUserName"&password=123
then check Authentication with call: http://localhost:8520/Authentication/CheckWhoAmI
//for request headers
if (OperationContext.Current.HttpClient.RequestHeaders.TryGetValue("content-length", out string[] contentLengthValues))
{
var value = contentLengthValues.FirstOrDefault();
}
//for response header example
OperationContext.Current.HttpClient.ResponseHeaders.Add("Content-Type", "application/json");