c# - 如果登录不正确,如何使 C# WCF session 无效

标签 c# wcf service authentication faultexception

我正在使用 WCF 为应用程序编写远程服务,其中登录信息保存在数据库中。该服务需要通过登录或帐户创建调用建立 session 。不涉及 ASP。

现在,当客户端通过调用公开的 IsInitiating 方法启动 session 时,我根据数据库中的信息检查提供的帐户数据,如果不正确,我想使它无效 session 并强制客户端通过调用 IsInitiating 方法重新启动。

查看some other问题,我发现了两种使 session 无效的方法的优缺点。一种是通过抛出 FaultException 来实现这一点;另一个以更温和的方式存储接受的 session ID。

现在,第一个虽然实现了我的期望,但过于激进,因为不正确的登录是应用程序正常流程的一部分。另一方面,第二个允许客户端继续调用非启动方法,即使它们将被拒绝,同时由于增加了线程安全要求,还会在服务上产生相当大的代码开销。

所以,问题是:是否有第三条路径允许服务使 session 初始化无效并将其传达给客户端,从而强制进行新的 IsInitiating 调用?

我拥有的代码的简化版本:

[DataContractAttribute]
public class AccountLoginFault
{
    public AccountLoginFault (string message)
    {
        this.Message = message;
    }

    [DataMemberAttribute]
    public string Message { get; set; }
}

[ServiceContract (SessionMode = SessionMode.Required)]
public interface IAccountService
{
    [OperationContract (
        IsInitiating = true)]
    [FaultContractAttribute (
        typeof (AccountLoginFault),
        ProtectionLevel = ProtectionLevel.EncryptAndSign)]
    bool Login (AccountData account, out string message);
}

[ServiceBehavior (
    ConcurrencyMode = ConcurrencyMode.Single,
    InstanceContextMode = InstanceContextMode.PerSession)]
public class AccountService : IAccountService
{
    public bool Login (AccountData account, out string message)
    {
        UserManager userdb = ChessServerDB.UserManager;
        bool result = false;
        message = String.Empty;

        UserData userData = userdb.GetUserData (account.Name);

        if (userData.Name.Equals (account.Name)
            && userData.Password.Equals (account.Password))
        {
            // Option one
            // Get lock
            // this.AcceptedSessions.Add (session.ID);
            // Release lock

            result = true;
        } else
        {
            result = false;

            // Option two
            // Do something with session context to mark it as not properly initialized.
            // message = "Incorrect account name or password. Account provided was " + account.Name;

            // Option three
            throw new FaultException<AccountLoginFault> (
                new AccountLoginFault (
                    "Incorrect account name or password. Account provided was " + account.Name));
        }

        return result;
    }
}

最佳答案

抛出异常是迄今为止最简单的选择,因为 WCF 强制 session 不能被重新使用。据我所知,您希望第三方组件完成的功能与此功能非常接近。但是,不是强制客户端再次调用 IsInitialized,而是强制客户端创建一个新连接。对我来说,这看起来差别很小。

另一种方法是拥有一个私有(private)变量 bool _authorised 并在每次方法调用时检查该变量。

关于c# - 如果登录不正确,如何使 C# WCF session 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3978281/

相关文章:

c# - 在 C# 中传递枚举数组

wcf - .net-WCF 我们如何以编程方式创建客户端?

c# - 将 OData Uri 转换为等效的 Linq 表达式

javascript - Restangular getList 不返回任何东西

c# - 有没有办法从 txt 文件 c# 中的数字中减去

c# - 多级 ConcurrentDictionary 仍然是线程安全的吗?

c# - 数据集更新时更新缓存

wcf - 使用 NLB 在 IIS 中托管自定义 WCF

c# - 在 Windows 服务中启动调试器

android - PhoneStateListener onSignalStrengthsChanged 停止在服务中被调用