我正在用.NET创建一个服务器,并为Android创建一个客户端应用程序.我想实现一种身份验证方法,它将用户名和密码发送到服务器,而服务器发回会话字符串.

我不熟悉水渍险,所以我会非常感谢你的帮助.

在java中,我编写了以下方法:

private void Login()
{
  HttpClient httpClient = new DefaultHttpClient();
  try
  {
      String url = "http://192.168.1.5:8000/Login?username=test&password=test";

    HttpGet method = new HttpGet( new URI(url) );
    HttpResponse response = httpClient.execute(method);
    if ( response != null )
    {
      Log.i( "login", "received " + getResponse(response.getEntity()) );
    }
    else
    {
      Log.i( "login", "got a null response" );
    }
  } catch (IOException e) {
    Log.e( "error", e.getMessage() );
  } catch (URISyntaxException e) {
    Log.e( "error", e.getMessage() );
  }
}

private String getResponse( HttpEntity entity )
{
  String response = "";

  try
  {
    int length = ( int ) entity.getContentLength();
    StringBuffer sb = new StringBuffer( length );
    InputStreamReader isr = new InputStreamReader( entity.getContent(), "UTF-8" );
    char buff[] = new char[length];
    int cnt;
    while ( ( cnt = isr.read( buff, 0, length - 1 ) ) > 0 )
    {
      sb.append( buff, 0, cnt );
    }

      response = sb.toString();
      isr.close();
  } catch ( IOException ioe ) {
    ioe.printStackTrace();
  }

  return response;
}

但在服务器端,到目前为止我还没有找到任何答案.

如果有人能解释如何用合适的应用程序创建合适的方法字符串登录(字符串用户名、字符串密码),我将非常感激.配置设置并与适当的[OperationContract]签名交互,以便从客户端读取这两个参数并使用会话字符串进行回复.

谢谢

推荐答案

要开始使用WCF,最简单的方法可能是只对Web服务绑定使用默认SOAP格式和HTTP POST(而不是GET).最简单的HTTP绑定是"basicHttpBinding".以下是您的登录服务的ServiceContract/OperationContract可能是什么样子的示例:

[ServiceContract(Namespace="http://mycompany.com/LoginService")]
public interface ILoginService
{
    [OperationContract]
    string Login(string username, string password);
}

服务的实现可能如下所示:

public class LoginService : ILoginService
{
    public string Login(string username, string password)
    {
        // Do something with username, password to get/create sessionId
        // string sessionId = "12345678";
        string sessionId = OperationContext.Current.SessionId;

        return sessionId;
    }
}

您可以使用ServiceHost将其作为Windows服务托管,也可以像普通ASP.NET Web(服务)应用程序一样将其托管在IIS中.对于这两种情况,有很多教程可供参考.

WCF服务配置可能如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>


    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="LoginServiceBehavior">
                    <serviceMetadata />
                </behavior>
            </serviceBehaviors>
        </behaviors>

        <services>
            <service name="WcfTest.LoginService"
                     behaviorConfiguration="LoginServiceBehavior" >
                <host>
                    <baseAddresses>
                        <add baseAddress="http://somesite.com:55555/LoginService/" />
                    </baseAddresses>
                </host>
                <endpoint name="LoginService"
                          address=""
                          binding="basicHttpBinding"
                          contract="WcfTest.ILoginService" />

                <endpoint name="LoginServiceMex"
                          address="mex"
                          binding="mexHttpBinding"
                          contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

(MEX内容对于生产是可选的,但对于使用WcfTestClient.exe进行测试和公开服务元数据是必需的).

您必须修改Java代码才能将SOAP消息发布到服务.在与非WCF客户端进行互操作时,WCF可能会有点挑剔,因此您必须稍微修改一下POST标头才能使其正常工作.一旦运行了这个程序,您就可以开始调查登录的安全性(可能需要使用不同的绑定来获得更好的安全性),或者可能使用WCF睡觉来允许使用GET而不是SOAP/POST进行登录.

下面是一个例子,从Java代码中可以看出HTTP POST应该是什么样子.有一个名为"Fiddler"的工具对于调试web服务非常有用.

POST /LoginService HTTP/1.1
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://mycompany.com/LoginService/ILoginService/Login"
Host: somesite.com:55555
Content-Length: 216
Expect: 100-continue
Connection: Keep-Alive

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Login xmlns="http://mycompany.com/LoginService">
<username>Blah</username>
<password>Blah2</password>
</Login>
</s:Body>
</s:Envelope>

.net相关问答推荐

dotnet 8 web api在部署到docker后无法工作

EFCore.DbSet.Update 方法添加新行而不是更新它

在接口内部声明 IEnumerable 而在具体类中声明 IList

Web API 中基于令牌的身份验证,无需任何用户界面

C#6.0 字符串插值本地化

Select 文件夹对话框 WPF

.Net 中的 Decimal.One、Decimal.Zero、Decimal.MinusOne 的用途是什么

如何将时间设置为当天的午夜?

如何将枚举值序列化为 int?

当程序员说针对接口而不是对象的代码时,他们是什么意思?

关闭 Visual Studio 中所有选项卡但当前选项卡的键盘快捷键?

如何保护我的 .NET 程序集免受反编译?

监听依赖属性的变化

为什么有些对象属性是 UnaryExpression 而有些是 MemberExpression?

场与财产.性能优化

如何找到二维数组的大小?

IEnumerable vs IReadonlyCollection vs ReadonlyCollection 用于公开列表成员

MailMessage,Sender 和 From 属性的区别

是否可以判断对象是否已附加到实体框架中的数据上下文?

忽略 LINQ to XML 中的命名空间