ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
2013-07-05 15:52
561 查看
在前一篇文章中,主要讨论了使用HTTP基本认证的方法,因为HTTP基本认证的方式决定了它在安全性方面存在很大的问题,所以接下来看看另一种验证的方式:digest authentication,即摘要认证。
ASP.NET Web API(二):安全验证之使用HTTP基本认证
ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
下图为摘要验证的验证原理流程图。
摘要验证实现的核心方法
实现完成后,使用摘要验证只需要在对应的方法加上[Authorize]属性标签即可。
但是永远没有绝对的安全,当用户使用字典进行穷举破解时,还是会存在一些被破解的隐患。
源代码下载
系列文章列表
ASP.NET Web API(一):使用初探,GET和POST数据ASP.NET Web API(二):安全验证之使用HTTP基本认证
ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
摘要认证原理
在基本认证的方式中,主要的安全问题来自于用户信息的明文传输,而在摘要认证中,主要通过一些手段避免了此问题,大大增加了安全性。下图为摘要验证的验证原理流程图。
namespace DigestAuthentication { public class AuthenticationHandler : DelegatingHandler { protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { try { var headers = request.Headers; if (headers.Authorization != null) { Header header = new Header(request.Headers.Authorization.Parameter, request.Method.Method); if (Nonce.IsValid(header.Nonce, header.NounceCounter)) { // Just assuming password is same as username for the purpose of illustration string password = header.UserName; string ha1 = String.Format("{0}:{1}:{2}", header.UserName, header.Realm, password).ToMD5Hash(); string ha2 = String.Format("{0}:{1}", header.Method, header.Uri).ToMD5Hash(); string computedResponse = String .Format("{0}:{1}:{2}:{3}:{4}:{5}", ha1, header.Nonce, header.NounceCounter, header.Cnonce, "auth", ha2).ToMD5Hash(); if (String.CompareOrdinal(header.Response, computedResponse) == 0) { // digest computed matches the value sent by client in the response field. // Looks like an authentic client! Create a principal. var claims = new List<Claim> { new Claim(ClaimTypes.Name, header.UserName), new Claim(ClaimTypes.AuthenticationMethod, AuthenticationMethods.Password) }; var principal = new ClaimsPrincipal(new[] { new ClaimsIdentity(claims, "Digest") }); Thread.CurrentPrincipal = principal; if (HttpContext.Current != null) HttpContext.Current.User = principal; } } } var response = await base.SendAsync(request, cancellationToken); if (response.StatusCode == HttpStatusCode.Unauthorized) { response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Digest", Header.UnauthorizedResponseHeader.ToString())); } return response; } catch (Exception) { var response = request.CreateResponse(HttpStatusCode.Unauthorized); response.Headers.WwwAuthenticate.Add(new AuthenticationHeaderValue("Digest", Header.UnauthorizedResponseHeader.ToString())); return response; } } } }
摘要验证实现的核心方法
实现完成后,使用摘要验证只需要在对应的方法加上[Authorize]属性标签即可。
摘要验证的优缺点
摘要验证很好地解决了使用基本验证所担心的安全性问题。但是永远没有绝对的安全,当用户使用字典进行穷举破解时,还是会存在一些被破解的隐患。
源码下载
编辑器里怎么找不到上传文件的地方了?我上传到了百度网盘里。源代码下载
相关文章推荐
- ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
- ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
- ASP.NET Web API:安全验证之使用摘要认证(digest authentication)
- [转]ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
- ASP.NET Web API(二):安全验证之使用HTTP基本认证
- ASP.NET Web API(二):安全验证之使用HTTP基本认证
- ASP.NET Web API(二):安全验证之使用HTTP基本认证
- ASP.NET Web API:安全验证之使用HTTP基本认证
- ASP.NET Web API(二):安全验证之使用HTTP基本认证
- ASP.NET Web API 安全验证之摘要(Digest)认证
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证
- Asp.Net MVC 4 Web API 中的安全认证---使用OAuth
- 浅析ASP.net Web API的Model验证(使用MVC4框架的Web API须谨慎)
- asp.net 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证
- ASP.NET Web API 应用教程(一) ――数据流使用
- Asp.Net使用加密cookie代替session验证用户登录状态 源码分享 欢迎拍砖
- 在asp.net使用web.config身份验证
- 使用 IIS 进行 Microsoft ASP.NET 2.0 成员/角色管理,第 1 部分:安全和配置概述
- asp.net中使用基于角色的身份Forms验证 (3)