简体   繁体   English

使用Windows应用程序中的表单身份验证的ASP.NET Web服务

[英]ASP.NET web service using forms authentication from a windows app

I have an ASP.NET web service that I can access via a windows program but now I want to secure the web service. 我有一个可以通过Windows程序访问的ASP.NET Web服务,但是现在我想保护Web服务的安全。 I can secure the web service using forms authentication. 我可以使用表单身份验证来保护Web服务。 How do you access the secured web service from a windows forms application? 如何从Windows窗体应用程序访问受保护的Web服务?

Although this is not the right approach, tt is theoretically possible to use forms authentication in the manner you describe. 尽管这不是正确的方法,但是从理论上讲,tt可以按照您描述的方式使用表单身份验证。 This could be accomplished by either: 这可以通过以下任一方法来完成:

  1. Using a WebRequest to send your requests in raw form to the web service. 使用WebRequest将原始请求发送到Web服务。 This will involve inspecting the response, extracting the relevant forms-authentication fields, and sending a response back which logs the user in. This will generate a cookie which you must send along with each subsequent response to the service 这将涉及检查响应,提取相关的表单身份验证字段,然后将响应发送回并登录用户。这将生成一个cookie,您必须将该cookie与随后对服务的每个后续响应一起发送
  2. Generate the FormsAuhentication authentication cookie yourself. 自己生成FormsAuhentication身份验证cookie。 This is complex as it involves synchronising the machine key on the calling application, and artificially manipulating the headers being sent to the machine hosting the service. 这很复杂,因为它涉及到在调用应用程序上同步机器密钥,并人为地操纵发送到托管服务的机器的标头。
  3. Display the forms-authentication form for the user to log in to at the beginning of a session requiring interaction with the web-service. 在需要与Web服务进行交互的会话开始时,显示供用户登录的表单身份验证表单。 You can then harvest the generated cookie and present it to the service in HTTP headers as in option (2). 然后,您可以像选项(2)一样,收集生成的cookie并将其显示在HTTP标头中给服务。

As you can see, these methods are highly complex, and are fundamentally a hack to use forms-authentication where it was never intended. 如您所见,这些方法非常复杂,从根本上来说,是使用表单身份验证的一种黑客手段,而从没有使用过。

Microsoft intended us to use either Windows authentication, or SSL certs to secure access to ASP.NET web services. Microsoft打算让我们使用Windows身份验证或SSL证书来保护对ASP.NET Web服务的访问。 See HTTP Security and ASP.NET Web Services on MSDN. 请参阅MSDN上的HTTP安全性和ASP.NET Web服务

If you are able to use WCF, then a few more options present themselves, including the ability to build a custom authentication mechanism into the SOAP, with some support from WCF. 如果您能够使用WCF,那么还会出现一些其他选择,包括在WCF的支持下将自定义身份验证机制构建到SOAP中的能力。

For the most part, securing web services is one of the trickiest parts of the job. 在大多数情况下,保护Web服务是工作中最棘手的部分之一。 Many live solutions which I have seen are compromises such as the ones above. 我看到的许多实时解决方案都是上述折衷方案。

It seems the answer is no. 看来答案是否定的。 Forms authentication is a cookie-based mechanism, and your WinForms app won't be able to hold and relay the cookies (without some serious workarounds, if at all). 表单身份验证是一种基于cookie的机制,您的WinForms应用将无法保存和中继cookie(如果没有,请采取一些严肃的解决方法)。

A potential workaround that I wrote up when researching your question attempted to use a NetworkCredential object, but that didn't work. 当研究您的问题时,我写了一个潜在的解决方法,尝试使用NetworkCredential对象,但是没有用。 Also tried was the ClientCredentials in .NET 4.0. 还尝试了.NET 4.0中的ClientCredentials

var ss = new MySecureWebService.MyServiceSoapClient();
ss.ClientCredentials.UserName.UserName = "abc";
ss.ClientCredentials.UserName.Password = "123";
string asmxReturn = ss.HelloWorld(); //exception returned here

The console app was still presented with the login html page when calling the webmethod. 调用Web方法时,控制台应用程序仍显示有登录html页面。

Other Suggestions 其他建议

  • If you have the source to your web service, extract its logic out into an assembly of its own. 如果您有Web服务的源代码,则将其逻辑提取到其自己的程序集中。 Reference that assembly in your WinForms app, and it's just as if you're calling the web service. 在WinForms应用程序中引用该程序集,就像在调用Web服务一样。

I understand that your goal is to reuse the app that's deployed, but the next best thing would be to use the same logic/implementation via .dll reference. 我了解您的目标是重用已部署的应用程序,但下一个最好的方法是通过.dll引用使用相同的逻辑/实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从ASP.NET Web应用程序到ASP.NET Web服务到SQL Server的身份验证 - Authentication from ASP.NET web app to ASP.NET web service to SQL Server 在iis上从asp.net web app调用windows服务 - Invoke windows service from asp.net web app on iis Forms身份验证应用程序中的ASP.NET Web服务 - ASP.NET Web Service inside Forms Authentication Application ASP.NET Web 表单和外部身份验证服务 - ASP.NET web forms and external authentication service 针对外部Web服务的ASP.NET MVC Forms身份验证 - ASP.NET MVC Forms authentication against external web service ASP.NET表单身份验证和调用Web服务 - ASP.NET Forms Authentication and Calling Web Service ASP.NET MVC 应用程序 Windows 身份验证在使用 Forms 身份验证后停止工作 - ASP.NET MVC App Windows Authentication stops working after using Forms Authentication 与使用ActiveDirectoryMembershipProvider的Forms身份验证相比,ASP.net Windows身份验证 - ASP.net Windows authentication compared to Forms authentication using ActiveDirectoryMembershipProvider ASP.Net Web窗体应用程序不通过Windows身份验证在服务器上进行身份验证 - 模拟失败 - ASP.Net Web Forms App does not authenticate on server with Windows Authentication - fails impersonate 如何从ASP.NET表单身份验证转换为Windows身份验证 - How to convert from asp.net forms authentication to windows authentication
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM