WCF服務用戶名密碼訪問

有2種方式,

第一直接在程序中指定用戶名密碼,配置調用

      private void BtnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                var client = new TicketListService.TicketListServicePortTypeClient();
                client.ClientCredentials.UserName.UserName = ConfigurationManager.AppSettings["UserName"];
                client.ClientCredentials.UserName.Password = ConfigurationManager.AppSettings["Password"];

                if (!string.IsNullOrEmpty(txtParam.Text.Trim()))
                {
                    string paramjson = txtParam.Text.Trim();
                    string datajson = client.queryTicketAllList(paramjson);
                    richTextBox1.Text = ConvertJsonString(datajson);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

Web.Config配置文件中添加對用戶名和密碼的標籤訪問

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings>
    <add key="UserName" value="admin"/>
    <add key="Password" value="123"/>
  </appSettings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="ITicketListServiceHttpBinding" >
          <security mode="TransportCredentialOnly" >
            <transport clientCredentialType="Basic"/>
            <message clientCredentialType="UserName"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://10.8.0.126/dxp/remote/execut"
        binding="basicHttpBinding" bindingConfiguration="ITicketListServiceHttpBinding"
        contract="TicketListService.ITicketListServicePortType" name="ITicketListServiceHttpPort" />
    </client>
  </system.serviceModel>
</configuration>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章