未提供C#商店应用程序Web服务用户名

未提供C#商店应用程序Web服务用户名,c#,web-services,windows-store-apps,C#,Web Services,Windows Store Apps,当我尝试访问Web服务时,会收到以下错误消息: 未提供用户名。在ClientCredentials中指定用户名 我确实创建了新凭据,如以下代码所示: public AppWebService_PortClient openConn2() { BasicHttpBinding binding = new BasicHttpBinding(); binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCre

当我尝试访问Web服务时,会收到以下错误消息:

未提供用户名。在ClientCredentials中指定用户名

我确实创建了新凭据,如以下代码所示:

public AppWebService_PortClient openConn2()
{
    BasicHttpBinding binding = new BasicHttpBinding();

    binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
    BasicHttpSecurityMode sMode = binding.Security.Mode;

    EndpointAddress adress = new EndpointAddress("myURL");

    AppWebService_PortClient client = 
        new AppWebService_PortClient(binding, adress);

    client.ClientCredentials.Windows.ClientCredential = 
        new System.Net.NetworkCredential("Username", "Password", "Domain");
    string userName = 
        client.ClientCredentials.Windows.ClientCredential.UserName;
    //client.ClientCredentials.Windows.AllowedImpersonationLevel 
        //= TokenImpersonationLevel.Delegation;

    return client;
}

如果我按照
string userName
读取凭据,我会得到正确的用户名。为什么会出现这种错误?

看起来有点像这个问题:

其中有以下“解决方案”。这可能适用于您的情况:

您是否在配置文件中正确配置了安全模式、clientCredentialtype?这里有一篇与你的问题非常吻合的博文。我希望这有帮助

实际上,我的申请有三个原因:

安全模式不合适。 客户端凭据类型不合适。 呼叫未传递所需的用户名和密码


你可能应该使用

client.ClientCredentials.UserName.UserName = "Username";
client.ClientCredentials.UserName.Password = "Password";

实际上,这听起来像是我的错误。但是博客上说我应该更改app.config文件。不过,我无法在C#store应用程序中找到它。所以它对我没有用处:)但是谢谢你的回复,真的,但是你也可以在代码中设置传输模式和凭证类型?我的意思是,尝试将安全模式设置为“无”。看看这是否有效。如果我将安全模式设置为“无”,我将得到一个错误:“提供的URI方案“https”无效;选择“http”。\r\n参数名称:via”要解决此问题,我必须将安全模式设置为:TransportWithMessageCredential