I have a WCF service like this:
[ServiceContract( SessionMode=SessionMode.Required)]
public interface IService
{
[OperationContract(IsInitiating = true, IsTerminating = false)]
void login(string id);
[OperationContract(IsInitiating = false, IsTerminating = false)]
string getdata();
}
public class Service : IService
{
public void login(string hashedid)
{
if (username != "someusername" || password != "somepassword")
{
// can not get data
}
else
{
// can get data
}
}
public string getdata()
{
return "these are data";
}
}
How can开发者_运维技巧 I write the method login and create the client application? Thanks you.
[ServiceContract( SessionMode=SessionMode.Required)]
public interface IService
{
[OperationContract(IsInitiating = true, IsTerminating = false)]
void login(string username, string password);
[OperationContract(IsInitiating = false, IsTerminating = false)]
string getdata();
}
public class Service : IService
{
// todo: make threadsafe!
public static List<Guid> authenticated = new List<Guid>();
public void login(string username, string password)
{
if (username == "correctUsername" || password == "correctPassword")
{
// user has given correct username/pasword
Guid currentSessionId = OperationContext.Current.SessionId;
// note: can throw Exception when calling login twice or more, check if item exists first
authenticated.Add(currentSessionId);
}
}
public string getdata()
{
Guid currentSessionId = OperationContext.Current.SessionId;
if (List.Contains(currentSessionId)
{
return "these are data";
}
return String.Empty;
}
}
You can identify a session by the current Session id. After a user authenticates correctly you can add this session to the list of authenticated session.
Mind: This is just some pseudo code. The session id should removed when the session is cloced, the list I use is not threadsafe,... But I hope this helps you get into the right direction.
精彩评论