简单登录 [英] Simple login

查看:57
本文介绍了简单登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的WCF服务:

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

您可以通过当前的会话ID来识别会话.用户正确验证身份后,您可以将此会话添加到已验证会话的列表中.

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.

注意:这只是一些伪代码.结束会话后,应该删除会话ID,我使用的列表不是线程安全的,但是我希望这可以帮助您朝正确的方向前进.

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.

这篇关于简单登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆