Asp.net 控制哪些WebMethods在WebService中对外公开
我想不出一个像样的头衔,所以让我先为此道歉 我有一个为我的应用程序编写的Web服务(称之为a),这样我就可以利用ASP.NET 3.5 AJAX功能。我使用生成的JavaScript代理进行AJAX调用 作为一个副作用,WebServicea公开给任何人作为对另一个项目的引用,这很好,除了我不希望某些WebMethods对外部应用程序可用(顺便说一句,在同一个域中) 所以我有两个问题:Asp.net 控制哪些WebMethods在WebService中对外公开,asp.net,security,web-services,asp.net-3.5,Asp.net,Security,Web Services,Asp.net 3.5,我想不出一个像样的头衔,所以让我先为此道歉 我有一个为我的应用程序编写的Web服务(称之为a),这样我就可以利用ASP.NET 3.5 AJAX功能。我使用生成的JavaScript代理进行AJAX调用 作为一个副作用,WebServicea公开给任何人作为对另一个项目的引用,这很好,除了我不希望某些WebMethods对外部应用程序可用(顺便说一句,在同一个域中) 所以我有两个问题: 有没有一种方法可以控制WebServicea中WebMethods的公开 如果没有,我想我只需要添加一个单
- 有没有一种方法可以控制WebServicea中WebMethods的公开
- 如何防止其他应用程序引用WebServiceA,同时仍允许其源应用程序访问它
此外,这里已经有一个类似的问题,没有任何好的答案。询问者描述了与我几乎相同的情况:出于安全目的,我会创建一个公共Web服务和一个私有Web服务。要创建该公共/私有Web服务,您可以将.asmx放入另一个文件夹,创建一个新的
web.config
文件,并在中定义您的授权用户,您可以在服务中使用自定义SOAP头,以要求将凭据传递给要保护的方法。这仍然会“暴露”这些方法,但它们是不可访问的。应用程序X将被允许访问所有方法,因为它将被设计为使用适当的安全头,但应用程序Y将被拒绝访问(尽管它将能够使用任何公共类型/枚举等)
我使用标准web服务和表单身份验证,如下所示:
' ************************************
' **** Example with Windows Forms ****
' ************************************
' Taken from http://www.dotnetbips.com/articles/dbd724e9-78f0-4a05-adfb-190d151103b2.aspx
' **** Login *************************
' Dim x As New localhost.Service1()
' Dim cc As New CookieContainer()
' Dim sessioncookie As Cookie
' Dim cookiecoll As New CookieCollection()
' x.CookieContainer = cc
' x.Login("user1", "password1")
' cookiecoll = x.CookieContainer.GetCookies
' (New Uri("http://localhost"))
' Session("sessioncookie") = cookiecoll("CookieName")
' **** Logout ************************
' Dim x As New localhost.Service1()
' Dim cc As New System.Net.CookieContainer()
' Dim sessioncookie As New System.Net.Cookie()
' x.CookieContainer = cc
' sessioncookie = CType(Session("sessioncookie"),
' System.Net.Cookie)
' If Not sessioncookie Is Nothing Then
' ' x.CookieContainer.Add(sessioncookie)
' End If
' x.Logout()
' Session.Remove("sessioncookie")
' ************************************
<WebMethod()> _
Public Function Login(ByVal UserName As String, ByVal Password As String) As Boolean
If UserName.Length > 0 And Password.Length > 0 Then
If FormsAuthentication.Authenticate(UserName, Password) Then
FormsAuthentication.SetAuthCookie(UserName, False)
Return True
End If
Else
Return False
End If
End Function
Public Sub ValidateAuthentication()
If Context.User.Identity.IsAuthenticated = False Then
Throw New System.UnauthorizedAccessException("User is not authenticated.")
End If
End Sub
<WebMethod()> _
Public Sub Logout()
If Context.User.Identity.IsAuthenticated = True Then
FormsAuthentication.SignOut()
End If
End Sub
'************************************
'****Windows窗体示例****
' ************************************
“摘自http://www.dotnetbips.com/articles/dbd724e9-78f0-4a05-adfb-190d151103b2.aspx
'****登录*************************
'Dim x作为新的localhost.Service1()
'Dim cc As New CookieContainer()
“像饼干一样的点心”
'Dim cookiecoll作为新CookieCollection()
'x.CookieContainer=cc
'x.Login(“user1”、“password1”)
'cookiecoll=x.CookieContainer.GetCookies
'(新Uri(“http://localhost"))
'Session(“sessionokie”)=cookiecoll(“CookieName”)
“****注销************************
'Dim x作为新的localhost.Service1()
'Dim cc As New System.Net.CookieContainer()
'Dim SessionOkie作为新系统.Net.Cookie()
'x.CookieContainer=cc
'sessionokie=CType(会话(“sessionokie”),
'系统.Net.Cookie)
“如果不是的话,那就什么都不是了
“”x.CookieContainer.Add(sessioncookie)
"完"
'x.注销()
'Session.Remove(“sessionokie”)
' ************************************
_
作为布尔值的公共函数登录(ByVal用户名作为字符串,ByVal密码作为字符串)
如果UserName.Length>0和Password.Length>0,则
如果是FormsAuthentication.Authentication(用户名、密码),则
FormsAuthentication.SetAuthCookie(用户名,False)
返回真值
如果结束
其他的
返回错误
如果结束
端函数
公共子验证身份验证()
如果Context.User.Identity.IsAuthenticated=False,则
抛出新系统。UnauthorizedAccessException(“用户未经身份验证”)
如果结束
端接头
_
公共子注销()
如果Context.User.Identity.IsAuthenticated=True,则
FormsAuthentication.SignOut()格式
如果结束
端接头
好吧,没错,这是我的想法,但如何使一个“私有”是我的问题。我最后做的是:我在解决方案中添加了一个包含“外部”服务的新项目,并将其发布到一个新位置,这样我就可以从主应用程序中单独控制身份验证和授权。