使用ObjectDataSource asp .net的Webservice凭据

时间:2013-01-18 06:19:11

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

我有一个问题:

我有webservice,需要用户名和密码(基本/ Windows身份验证)

我使用“添加网络参考”(正常的东西......右)在Visual Studio 2010中设置了一个Web项目

这是ASPX代码:

<asp:Repeater ID="repUnserName" runat="server" DataSourceID="objGetUserName">
    <ItemTemplate>
        Name: <span class="info">
            <%# Eval("myName")%></span>
    </ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="objGetUserName" runat="server" SelectMethod="getUserNameByEmail"
    TypeName="SecureService" OnSelecting="objGetUserName_Selecting">
    <SelectParameters>
        <asp:Parameter DefaultValue="" Name="UserEmail" Type="String" />
    </SelectParameters>
</asp:ObjectDataSource>

c#代码:

protected void objGetUserName_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {            
            e.InputParameters["UserEmail"] = "myemail@email.com 

    }

现在我的问题是如何在使用ObjectDataSource调用getUserByEmail方法时传递webservice登录凭据?

感谢您的提前帮助

2 个答案:

答案 0 :(得分:0)

我建议在页面(或某些帮助程序类)中使用包装器方法来控制凭据传递。例如,

<asp:ObjectDataSource ID="objGetUserName" runat="server" SelectMethod="getUserNameByEmail"
    TypeName="PageClassName" OnSelecting="objGetUserName_Selecting">

internal static DataTable getUserNameByEmail(string email)
{
    var proxy = new SecureService();
    proxy.Credentials = new NetworkCredentials("user-name", "password");
    return proxy.getUserNameByEmail(email);
}

免责声明:未经测试的代码 - 只是为了获得解决方案的想法

答案 1 :(得分:0)

这就是我最终要做的事情:

创建“对象创建事件”

示例代码:

protected void MyObjDS_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
MyWebService.Service webProxy = new MyWebService.Service();
webProxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
e.ObjectInstance = webProxy;
}

来源网址:http://paulsiu.wordpress.com/2008/08/31/error-401-when-using-objectdatasource-to-call-a-web-service/#comment-4023