简单登录

时间:2010-05-04 06:56:44

标签: wcf login

我有这样的WCF服务:

[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";
    }
}

如何编写方法登录并创建客户端应用程序? 谢谢你。

1 个答案:

答案 0 :(得分:7)

[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;
    }
}

您可以按当前会话ID识别会话。用户正确进行身份验证后,您可以将此会话添加到经过身份验证的会话列表中。

介意:这只是一些伪代码。当会话被cloced时,会话ID应被删除,我使用的列表不是线程安全的......但我希望这可以帮助你找到正确的方向。