写在前面
在前面的三篇文章,已经介绍了restful风格wcf,如何实现增删改查以及文件的上传下载操作。本篇文章将介绍一下,调用restful的权限认证的内容。在调用的接口,为了安全,总会需要对请求进行权限认证的。以防一些非法的操作。
系列文章
一个例子
在REST WCF中,我们可以利用 HttpHeader 来完成这一目标。
首先我们添加一个校验身份的一个方法。
/// <summary>
/// 校验是否有权限访问
/// </summary>
/// <returns></returns>
private bool CheckIsCheckAuthorization()
{
//获得当前web操作上下文
WebOperationContext woc = WebOperationContext.Current;
//获得当前请求头中的Authorization
var auth = woc.IncomingRequest.Headers[System.Net.HttpRequestHeader.Authorization];
//如果auth为空,或者不等于admin/123,则响应405 MethodNotAllowed
if (string.IsNullOrEmpty(auth) || auth != "admin/123")
{
woc.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.MethodNotAllowed;
return false;
}
return true; }
然后,在每个接口中,加上权限认证的语句。
/// <summary>
/// 获得所有的用户信息
/// </summary>
/// <returns>json或者xml</returns>
public List<UserInfo> QueryList()
{
if (CheckIsCheckAuthorization())
{
return new List<UserInfo>()
{
new UserInfo() { ID = , Name = "wofly", Age = , Birthday = DateTime.Now, Gender = true },
new UserInfo() { ID = , Name = "san zhang", Age = , Birthday = DateTime.Now, Gender = true },
new UserInfo() { ID = , Name = "wukong sun", Age = , Birthday = DateTime.Now, Gender = false },
new UserInfo() { ID = , Name = "zi ma", Age = , Birthday = DateTime.Now, Gender = true }
};
}
else
{
return null;
}
}
在浏览器中浏览,http://localhost:21074/userInfo/api/users
我们现在使用postman模拟请求,进行验证,如图所示:
这样,就对可以控制对接口的访问,只有有权限的用户才可以访问。但是,问题来了,有那么多的方法,每个方法都要加上那么一句权限的认证,看起来相当的繁琐。这里介绍一种高大上的方式。通过在WebServiceHostFactory中的拦截请求上下文中Authorization可以做到。
public class SecureWebServiceHostFactory : System.ServiceModel.Activation.WebServiceHostFactory
{
public override System.ServiceModel.ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
{
var host= base.CreateServiceHost(constructorString, baseAddresses);
//在这里,拦截验证
host.Authorization.ServiceAuthorizationManager = new MyServiceAuthorizationManager();
return host;
}
protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
var host = base.CreateServiceHost(serviceType, baseAddresses);
//在这里,拦截验证
host.Authorization.ServiceAuthorizationManager = new MyServiceAuthorizationManager();
return host;
}
}
/// <summary>
/// 自定义验证方式
/// </summary>
public class MyServiceAuthorizationManager : ServiceAuthorizationManager
{
protected override bool CheckAccessCore(OperationContext operationContext)
{
var woc = System.ServiceModel.Web.WebOperationContext.Current;
var auth = woc.IncomingRequest.Headers[HttpRequestHeader.Authorization];
if (string.IsNullOrEmpty(auth) || auth != "admin/123")
{
woc.OutgoingResponse.StatusCode = HttpStatusCode.MethodNotAllowed;
return false;
}
return true;
}
}
然后,在注册路由的时候,做一下修改:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
//注册路由
System.Web.Routing.RouteTable.Routes.Add(
new System.ServiceModel.Activation.ServiceRoute(
"userInfo",
new SecureWebServiceHostFactory(), typeof(UserService)
));
//注册路由
System.Web.Routing.RouteTable.Routes.Add(new System.ServiceModel.Activation.ServiceRoute(
"imageService", new System.ServiceModel.Activation.WebServiceHostFactory(), typeof(ImageService)));
}
}
上面代码中,红色的部分,就是自定义的认证的类。
现在我们使用postman模拟请求,进行一下验证,
总结
好了,关于restful风格wcf的使用方式,就介绍到这里,希望对你有所帮助。
参考文章:
http://blog.csdn.net/fangxing80/article/details/6263780