【问题标题】:How to make sure security for Webservice?如何确保 Webservice 的安全性?
【发布时间】:2013-11-14 18:45:43
【问题描述】:

我有一个 Web 应用程序(在 VS2008 C# ASP.NET 3.5 Framework 中)。在我的登录页面中有一个函数 CheckLogin() 执行登录功能。我使用了远程网络服务。 objWeb 是该 Web 服务的对象。 WebService_CheckLogin 是我的远程 Web 服务中的一个 Web 方法。数据库连接字符串写在我的 websservice 的类文件中。

public DataSet CheckLogin()
    {
        string username = Convert.ToString(txtUname.Text);
        string password = Convert.ToString(txtPassword.Text);
        return objWEB.WebService_CheckLogin(username,password);

    }

我的网络服务中的网络方法

[WebMethod]
    public DataSet WebService_CheckLogin(string uname,string pswd)
        {
            c.connect();
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand("sp_verifyuser", c.con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Username", uname);
            cmd.Parameters.AddWithValue("@Password", pswd);
            c.getdataset(cmd, ref ds);
            return ds;
        }

我在 webservice 中的连接类

public void connect()
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();

        }
        con.ConnectionString="Data Source=xxxxxxx;Initial Catalog=xx;User ID=xx;Password=xxxx";

        con.Open();
    }

我的问题是“安全方面有什么问题吗”?我的意思是“任何人都可以通过其网址访问我的网络服务”吗?我有许多其他网络方法,其中我将字符串作为参数传递,例如

string profilePassword = objWEB.Verify_ProilePassword("exec sp_verify_profilepwd '" + txt_profil_pwd.Text + "','"+cid+"'");

问候, 大卫

【问题讨论】:

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


    【解决方案1】:

    这完全取决于您如何保护 WebMethods。您上面的代码可能会检查用户是否拥有有效的用户名/密码组合,但很难从那里判断您在用它做什么。

    在为 WebMethod 设置了 authenticated a user 和 EnableSession 之后,您可以执行以下操作:

    [WebMethod(EnableSession = true)]
    public static string HelloWorld(){
    if (User.Identity.IsAuthenticated)
    {
        return "Hello " + HttpContext.Current.Request.User.Identity.Name;
    }
    else
    {
        return "I don't talk to strangers";
    }
    }
    

    如有任何问题,请随时发表。

    另外 - 如果您对 sp_verifyuser 存储过程不小心,这样的调用可能会导致灾难:

    WebService_CheckLogin("*","*")
    

    【讨论】:

    • @Greg...那么我的 CheckLogin() 应该如何?
    猜你喜欢
    • 2015-04-02
    • 2018-07-28
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多