【问题标题】:What is the best way to secure a webservice?保护 Web 服务的最佳方法是什么?
【发布时间】:2011-01-17 07:56:10
【问题描述】:

正如标题已经解释的那样,我想保护我的网络服务。 我读过您可以使用soap身份验证标头来执行此操作,但是用户名和密码以纯文本形式传递。

我想知道我应该做些什么来保护我的网络服务? 例子会很棒。

我有一个与我们合作的公司的例子,它有 2 个网络服务。 一个负责安全,一个负责获取所需的数据,但我没有他们的代码,但系统看起来很棒:

bool loginSuccessFull = false;

/// knooppunt
string loginID = ConfigurationManager.AppSettings["WebServiceLogin"];
string password = ConfigurationManager.AppSettings["WebServicePass"];


//A. The m_SecurityService object is created and initialised
Security securityService = new Security();
securityService.CookieContainer = new System.Net.CookieContainer();


string challenge = securityService.InitializeLogin(loginID);
string pwd = password;
string response = pwd + challenge;


System.Security.Cryptography.SHA1CryptoServiceProvider SHA1 = new System.Security.Cryptography.SHA1CryptoServiceProvider();
SHA1.Initialize();
byte[] hash = SHA1.ComputeHash(System.Text.Encoding.Default.GetBytes(response));

System.Text.StringBuilder builder = new System.Text.StringBuilder();
foreach (byte b in hash)
    builder.Append(b.ToString("x2"));

//2. A login is done with the m_SecurityService object
if (securityService.Login(builder.ToString()))
{
    string ssoToken = Request.QueryString["SSOTOKEN"];
    string ssoID = Request.QueryString["SSOID"];
    if (!String.IsNullOrEmpty(ssoToken) && !String.IsNullOrEmpty(ssoID))
    {
        // Check with webserice if the token is valid.
        Knooppunt.SSO.GenericSSO sso = new Knooppunt.SSO.GenericSSO();
        sso.CookieContainer = securityService.CookieContainer;
        try
        {
            if (sso.validateSSOToken(Convert.ToInt32(ssoID), ssoToken))
            {
                loginSuccessFull = true;
                FormsAuthentication.RedirectFromLoginPage("default user", false);
            }
        }
        catch
        { }
    }
}

【问题讨论】:

    标签: c# .net asp.net security web-services


    【解决方案1】:

    如果它真的是一个网络服务,你应该使用Windows Communication Foundation 来生成代理并进行调用。它使很多代码变得非常容易。

    老实说,用于连接到您正在使用的 Web 服务(SSO?)的包看起来非常不标准,并且无非是派生自 HttpWebRequest,这是非常低级的,而且使用起来太复杂了。

    如果您要保护自己的 Web 服务(并且通过 HTTP 通道公开它),最简单的方法是为您的主机获取数字证书,然后使用基于 HTTPS 的基本 HTTP 身份验证。

    您还可以使用WS-Security specifications 的其他方面(例如对消息进行编码等)来保护您的服务。

    请注意,WCF 支持所有这些选项,因此您无需进行任何开箱即用的编码,也可以将其托管在 IIS 中。

    对 WCF 的一个很好的初学者参考是 Michelle Bustamante's "Learning WCF: A Hands-On Guide"

    之后,对于更高级的 WCF 内容(尤其是如果您想了解围绕 WCF 和 WS-* 中的安全性的概念)我强烈推荐"Programming WCF Services" by Juval Lowy

    【讨论】:

    • 完全同意。无需重新发明轮子。 WCF 正是您所要求的。
    • @Sem Dendonker:你能使用 .NET 3.0 吗? WCF 在 .NET 3.0 中,这是一个纯粹的附加解决方案(只添加了库,没有对基类库或 CLR 进行更改),因此如果您使用的是 .NET 2.0,它应该很容易。
    • 我的 WS 将从 AS400 服务器访问。在这种情况下,WCF 会起作用吗?
    猜你喜欢
    • 1970-01-01
    • 2020-09-26
    • 2011-11-20
    • 1970-01-01
    • 2021-04-10
    • 2010-09-22
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    相关资源
    最近更新 更多