未使用SetAuthCookie和完整的.net客户端维护对WCF服务的身份验证,但可与Silverlight一起使用 [英] Authentication to a WCF service not maintained using SetAuthCookie and a full .net client but works with silverlight

查看:94
本文介绍了未使用SetAuthCookie和完整的.net客户端维护对WCF服务的身份验证,但可与Silverlight一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个启用了表单身份验证和AspNetCompatibility的站点.实际的客户端是Silverlight应用程序,并且运行良好,但是我想使用普通的.net单独对应用程序进行单元测试以行使服务(实时系统,用于无副作用的方法).但是,当我停止使用Silverlight并开始使用完整的.net时,它就不会保持身份验证

I've setup a site with forms authentication and AspNetCompatibility enabled. The actual client is a silverlight application, and works fine, however I want to unit test the application separately using normal .net to exercise the service (live system for methods without side-effects). However, when I stop using Silverlight and start using full .net it doesn't remain authenticated

在Web服务中,我有:

In a web service I have:

[OperationContract]
public bool Login(string Username, string Password, bool isPersistent)
{
    if (Membership.ValidateUser(Username, Password))
    {
        FormsAuthentication.SetAuthCookie(Username, isPersistent);
        return true;
    }
    else
    {
        return false;
    }
}

[OperationContract]
public bool IsLoggedIn()
{
    return HttpContext.Current.User.Identity.IsAuthenticated;
}

然后在客户端的测试方法中,我这样称呼它:

Then in a test method on the client I call it like so:

Assert.IsTrue(Client.Login("MyUsername","MyPassword", true));
Assert.IsTrue(Client.IsLoggedIn());

客户端是.net自动生成的ServiceReference客户端的实例.第一个断言通过,但是第二个断言失败,即从一个方法调用到下一个断言将停止登录.Silverlight应用程序中的类似方法将通过.

The client is an instance of the automatically generated ServiceReference client for .net. The first assertion passes but the second one fails, i.e. from one method call to the next it stops being logged in. A similar method in the silverlight application would pass.

如何使正常的.net像Silverlight那样正确运行?是否有更好的方法为全.net配置客户端/服务?

How can I make normal .net behave correctly as silverlight would? Is there just a better way of configuring client/service for full .net?

请求的附加信息

服务配置:

<services>
  <service behaviorConfiguration="MyBehaviour" name="SSCCMembership.Web.Services.LoginService">
    <endpoint address="" binding="customBinding" bindingConfiguration="customBindingBinary"
      contract="SSCCMembership.Web.Services.LoginService" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<bindings>
  <customBinding>
    <binding name="customBindingBinary">
      <binaryMessageEncoding />
      <httpTransport />
    </binding>
  </customBinding>
</bindings>
<behaviors>
  <serviceBehaviors>
    <behavior name="MyBehaviour">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

身份验证配置:

<authentication mode="Forms" />
<membership defaultProvider="OdbcProvider">
  <providers>
    <clear />
    <add name="OdbcProvider" type="SSCCMembership.Web.SimpleMembershipProvider" applicationName="/SSCCMembership" requiresUniqueEmail="false" connectionStringName="mainConn" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="true" writeExceptionsToEventLog="true" />
  </providers>
</membership>

然后我使用以下内容在WPF中构建客户端

I then use the following to construct the client in WPF

public static T LoadService<T>(string URI, Func<CustomBinding, EndpointAddress, T> F)
{
    try
    {
        Uri U = new Uri(new Uri(Root), URI);

        BinaryMessageEncodingBindingElement binary = new BinaryMessageEncodingBindingElement();
        HttpTransportBindingElement transport;
        if (U.Scheme == "http")
            transport = new HttpTransportBindingElement();
        else if (U.Scheme == "https")
            transport = new HttpsTransportBindingElement();
        else
            throw new Exception(U.Scheme + " is not a recognised URI scheme");
        transport.MaxBufferSize = int.MaxValue;
        transport.MaxReceivedMessageSize = transport.MaxBufferSize;
        transport.AllowCookies = true;

        CustomBinding binding;
        binding = new CustomBinding(binary, transport);


        EndpointAddress address = new EndpointAddress(U);

        return F(binding, address);
    }
    catch (Exception)
    {
        return default(T);
    }
}

我这样称呼:

var Client = LoadService(ServiceLocation, (b,e)=>new LoginServiceClient(b,e));

推荐答案

我认为您遇到的主要问题是由于每个应用程序在其中运行的上下文所致.

I think the main problem you are having is due to the context that each application runs in.

Silverlight应用程序在其托管的网页上下文中运行-因此,它可以维护会话信息,Cookie以及您无需手动担心的所有其他事情.

A Silverlight application runs in the context of the web-page it's hosted in - and thus, can maintain session info, cookies, and all sorts of other things that you don't need to manually worry about.

一旦连接到在浏览器外部运行的应用程序,您的上下文(就asp.net而言)便发生了变化.您是否尝试过在浏览器外启用/安装silverlight应用程序以查看是否存在相同的错误?

As soon as you connect with an application running outside of the browser, your context (as far as asp.net is concerned) has changed. Have you tried enabling/installing the silverlight application out-of-browser to see if you have the same errors?

解决此问题的一种方法是使用Silverlight单元测试项目并将其连接到Web服务以进行测试. (我已经为所工作的公司成功地使用了企业silverlight应用程序完成了此任务.)Silverlight单元测试项目生成了Silverlight应用程序,该应用程序可以托管在网页上并从该网页运行-与任何其他Silverlight应用程序相同,就ASP.net而言,使您的网络服务调用因上下文而失败的可能性大大降低.

One way to work around this is to use the Silverlight Unit Test project and connect that to your web service for testing. (I have successfully done this with enterprise silverlight apps for the company I work for) The silverlight unit test project results in a silverlight app that can be hosted on a web page, and run from there - in the same context as any other silverlight app, as far as ASP.net is concerned, making your web-service calls much less likely to fail due to context.

有关详细信息,请查看 Silverlight单元测试框架首页.

Check out the Silverlight Unit Test Famework Homepage for more info...

这篇关于未使用SetAuthCookie和完整的.net客户端维护对WCF服务的身份验证,但可与Silverlight一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆