面向对象的登录功能 [英] Object oriented Login functionality

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

问题描述

用户登录功能是许多应用非常普遍。我想看看人们如何实现面向对象的方式这个功能。

User Login functionality is very common to many applications. I would like to see how people implement this functionality in Object oriented way.

我有一个用户,我需要验证对系统中的用户ID和密码(这可能是LDAP,数据库等)。所以,你会创造什么样的类和操作来实现这一功能呢?

I have a User and I need to validate the userId and password against a system(this could be ldap, database, etc.). So what kind of classes and operations you would create to achieve this functionality?

还是OO一个不错的选择来开发此功能?

Or is OO a bad choice to develop this functionality?

我将要开始一个新的项目,所以要收集好的选择。

I am about to start a new project so want to gather good options.


我知道有哪些已经提供这种解决方案的框架。我曾在早期项目中使用它们。我是想看到的是人们如何在OO方式实现这一点。

I know there are frameworks which provide this solution already. I have used them in earlier projects. What I was trying to see is how people implement this in OO way.

我读的答案,每个人都建议单独证书和认证服务。如果不是证书我用类名作为用户则不宜使用类应该有一个叫做登录方法?就像一个Person对象将有一个方法饮料代替DrinkService或者我是错的正确认识呢?

I read the answers and everybody suggested a separate Credentials and Authentication Service. If instead of Credentials I use class name as User then shouldn't User class should have a method called login? Just like a Person object will have a method drink instead of DrinkService or I am wrong in understanding this correctly?

推荐答案

究竟如何扩展它需要是什么?我会定义一个抽象类,凭证,它封装了一个特定系统的需要验证信息。它的子类为特定系统类型。一个例子是仅包含用户名和密码BasicCredentials。然后,定义用于定义身份验证方法的接口。也许我还定义了包括额外的主机信息的抽象类的主机。这可能是太多抽象,这取决于你预想一下进行身份验证。

Exactly how extensible does it need to be? I'd define an abstract class, Credentials, that encapsulates the needed authentication information for a given system. Subclass it for specific system types. An example would be BasicCredentials that contains only username and password. Then, define an interface that defines methods for authentication. Maybe I'd also define an abstract Host class that includes additional host information. This may be too much abstraction, depending on what you envision authenticating against.

这个例子code是C#3.0。

This example code is C# 3.0.

public abstract class Credentials
{
}

public class BasicCredentials : Credentials
{
    public String Username { get; set; }
    public String Password { get; set; }
}

public abstract class Host
{
}

public class IPHost : Host
{
    public IPAddress Location { get; set; }
}

public interface IAuthenticate
{
    bool Authenticate(Credentials creds, Host host);
}

public class BasicAuthenticator : IAuthenticate
{
    public bool Authenticate(Credentials creds, Host host)
    {
        // Check to make sure we're given the right type of parameters
        if (creds is BasicCredentials && host is IPHost)
        {
            // Do your magic here
        }
    }
}

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

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