Silverlight+ASP.NET MVC+表单身份验证 = WCF 中没有身份验证

发布于 2024-11-06 09:02:52 字数 1281 浏览 1 评论 0 原文

我有一个带有表单身份验证的标准 ASP.NET MVC 站点。 用户通过网页登录。然后他们就可以访问 silverlight 应用程序。当他们登录时,我向他们传递一个表单身份验证 cookie,就像任何其他站点一样:

 FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);

XAP 文件位于 ~/ClientBin/ 文件夹中。 SVC 文件位于 ~/Services/ 文件夹中。匿名访问被阻止:

<configuration>
   <system.web>
     <authorization>
       <deny users="?"/>
     </authorization>
   </system.web>
</configuration>

Silverlight 对 SVC 的调用会导致 HTTP 302 弹回登录页面,并使 Silverlight 崩溃。 :( 仅当禁用匿名访问时才会发生这种情况。我认为 Silverlight 不会随 WCF 服务调用一起传递 cookie。为什么不呢?

我确保使用文档中定义的 AspNetCompatibilityRequirements (http://msdn.microsoft.com/en-us/library/dd560702(VS.95).aspx):

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class TaskSchedulerService : ITaskSchedulerService
{

并且我的 web.config

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>

使用 basicHttpBinding 或 customBinding 会发生同样的事情启用二进制压缩。

I have a standard ASP.NET MVC site with forms authentication.
Users log in via a web page. Then they can reach a silverlight app. When they log in, I pass them a forms auth cookie just like any other site:

 FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);

The XAP file is in the ~/ClientBin/ folder. The SVC file is in the ~/Services/ folder. Anonymous access is blocked:

<configuration>
   <system.web>
     <authorization>
       <deny users="?"/>
     </authorization>
   </system.web>
</configuration>

The Silverlight calls to the SVC results in a HTTP 302 bouncing back to the login page, and crashing the Silverlight. :( This only happens when anonymous access is disabled. I think Silverlight is NOT passing the cookie along with the WCF service call. Why not?

I made sure to use the AspNetCompatibilityRequirements as defined within the documentation (http://msdn.microsoft.com/en-us/library/dd560702(VS.95).aspx):

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class TaskSchedulerService : ITaskSchedulerService
{

and my web.config has

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>

The same thing happens using basicHttpBinding or customBinding with BinaryCompression enabled.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心头的小情儿 2024-11-13 09:02:52

好消息!我发现了这个问题...
我的 silverlight 应用程序经过编程,可以注入一个点“.”。调用 localhost 上的 Web 服务时输入 URL
(http://localhost/myservice.svc -> http://localhost./myservice.svc) 。

我特意添加了这个额外的点,以便我可以使用 fiddler 调试本地流量。我删除了点,身份验证工作了!额外的点被视为一个单独的域。

事实证明,使用 INTERNET EXPLORER 9,FIDDLER 可以在没有点的情况下调试本地流量。

正如 MSDN 文档所说:

要从 Silverlight 应用程序使用该服务,不需要特殊步骤。调用服务的方式与调用非安全服务的方式相同。调用该服务时,如果用户未登录到托管 Silverlight 应用程序的 ASP.NET 站点,或者用户无权调用该服务,则会发生错误。因此,在使用安全服务时妥善处理错误情况尤为重要。

底线:如果您正在开发 silverlight,IE9 是您最好的朋友。

Good news! I found the issue...
My silverlight application was programmed such that it would inject a dot "." into the URL when it called the web service on localhost
(http://localhost/myservice.svc -> http://localhost./myservice.svc).

I purposefully added this extra DOT so I could debug local traffic with fiddler. I removed the dot, and authentication works! The extra dot was percieved as a separate domain.

It turns out that WITH INTERNET EXPLORER 9, FIDDLER CAN DEBUG LOCAL TRAFFIC WITHOUT THE DOT.

As the MSDN Dcoumentation says:

To use the service from a Silverlight application, no special steps are required. Invoke the service in the same way as you would invoke a non-secure service. When calling the service, if the user is not logged in to the ASP.NET site that hosts your Silverlight application, or if the user is not authorized to call the service, an error will occur. Thus, it is especially important to gracefully handle error conditions when using secure services.

Bottom line: if you are developing silverlight, IE9 is your best friend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文