0

我有个问题:

我有需要用户名和密码的网络服务(基本/Windows 身份验证)

我在 Visual Studio 2010 中使用“添加 Web 引用”设置了一个 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 方法时传递 Web 服务登录凭据?

提前感谢您的帮助

4

2 回答 2

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);
}

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

于 2013-01-18T08:11:50.157 回答
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

于 2013-01-21T23:17:25.500 回答