wcf - 简单登录

标签 wcf authentication

我有一个像这样的 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";
    }
}

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

最佳答案

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

您可以通过当前 session ID 来识别 session 。用户正确验证后,您可以将此 session 添加到已验证 session 列表中。

注意:这只是一些伪代码。关闭 session 时应删除 session ID,我使用的列表不是线程安全的,...但我希望这可以帮助您进入正确的方向。

关于wcf - 简单登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2763384/

相关文章:

java - HttpURLConnection 采用登录用户而不是使用提供的凭据来连接 Web 服务

testing - 具有设计身份验证的 rspec Controller 测试

c# - 将 WCF 配置文件转换为代码

c# - WCF 并行模拟

c# - Serilog WCF 在没有构造函数的情况下使用?

Spring Security - UserDetailsS​​ervice 实现 - 登录失败

java - 我在 camel 下使用 apache shiro,我无法将组映射到具有 ldap/active 目录的角色

php登录代码,数据不匹配

c# - 尝试使用 MEF 将依赖项注入(inject) IIS 托管的 WCF 服务

javascript - 通过ajax调用webservice方法