Ale*_*940 3 c# wcf soap web-services azure
我在Azure应用程序上托管了一个SOAP WCF.此服务仅由服务器使用,不包含UI.我只需要一个服务帐户来授权我的WCF.我不能使用oauth,因为它是SOAP.我已经阅读了一些关于ACS的内容,但在我的案例中似乎有些过分,因为我只想使用一个帐户来保护我的WCF.我的想法是,我将利用Azure AD在那里建立一个服务帐户并使用它来保护服务.
这甚至可以在Web应用程序上使用,还是需要将其托管在Web角色上?在任何情况下,我如何根据我的前提在我的WCF上实现简单的安全性?
详细的答案示例
经过一般性讨论后,这里有一个建立传输安全性+简单密码的详细示例(在IIS中,内部或Azure我刚测试过)
这很简单.
- 没有角色,没有基于身份的声明或程序控制.
- 身份是硬编码的.
- 没有使用更强大的消息安全性(中间的人).
- 传输安全性是最小的,因为基本身份验证没有安全.
该安全方案实施起来很短
1.创建具有传输安全性的Web服务
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicBindingConfiguration">
<security mode="Transport">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="HelloServiceLibrary.HelloService" behaviorConfiguration="customIdentificationBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract ="HelloServiceLibrary.IHelloService"
name="basicEndpoint"
bindingConfiguration="BasicBindingConfiguration">
</endpoint>
Run Code Online (Sandbox Code Playgroud)
2.声明查找Basic-Auth的模块
<system.webServer>
<modules>
<add name="BasicAuthenticationModule"
type="Security.UserNameModuleAuthenticator,App_Code/Security" />
</modules>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
3.模块的实施:
public class UserNameModuleAuthenticator : IHttpModule{
...
public void OnAuthenticateRequest(object source, EventArgs eventArgs){
HttpApplication app = (HttpApplication)source;
string authStr = app.Request.Headers["Authorization"];
string username = ...; // from header
string password = ...; // from header
if (username == "gooduser" && password == "password")
{
app.Context.User = new GenericPrincipal(new GenericIdentity(username, "Custom Provider"), null);
}
else
{
DenyAccess(app);
return;
}
Run Code Online (Sandbox Code Playgroud)
4配置客户端以传递基本身份验证
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicEndpoint">
<security mode="Transport" >
<transport clientCredentialType="Basic"
proxyCredentialType="None"
realm="" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://localhost/TransportUsernameService/HelloService.svc"
binding="basicHttpBinding" bindingConfiguration="basicEndpoint"
contract="SecureServiceReference.IHelloService" name="basicEndpoint" />
</client>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
5.在客户端传递**凭据到服务器**
HelloServiceClient client = new HelloServiceClient("basicEndpoint",
new EndpointAddress("https://testsecurewebservice.azurewebsites.net/HelloService.svc"));
client.ClientCredentials.UserName.UserName = userName;
client.ClientCredentials.UserName.Password = password;
String msg = client.SayHello(userName);
Run Code Online (Sandbox Code Playgroud)
可能的扩展
[PrincipalPermission(SecurityAction.Demand, Role = "Manager")]
Run Code Online (Sandbox Code Playgroud)
这里有完整的解决方案:http: //1drv.ms/1Q5j9w0
问候
| 归档时间: |
|
| 查看次数: |
1369 次 |
| 最近记录: |