asp.net-mvc-4 – 使用基本HTTP身份验证的MVC4 app WebApi中的Si
我正在尝试实现具有以下要求的MVC4 Web应用程序:
(a)它仅向经过身份验证的用户提供服务.至于身份验证,我想使用简单的成员身份,因为它是MVC的最新身份验证技术,使我能够定义自己的数据库表,提供开箱即用的OAuth支持,并且可以轻松地与MVC和的WebAPI. (b)它通过WebApi为移动/ JS客户端公开一些核心功能,这些功能应通过基本HTTP身份验证(SSL)进行身份验证.通常,我会让JS客户端使用对WebApi控制器的jQuery AJAX调用,使用不同用户角色的Authorize属性进行修饰. (c)理想情况下,在混合环境中我想避免双重身份验证:即,如果用户已通过浏览器进行身份验证,并且正在访问暗示对WebApi控制器操作进行JS调用的页面,则(a)机制应该是足够. 因此,虽然(a)由默认的MVC模板覆盖,但(b)需要基本的HTTP身份验证而不需要浏览器的中介.为此,我应该创建一个DelegatingHandler,就像我在这篇文章中找到的那样:http://www.piotrwalat.net/basic-http-authentication-in-asp-net-web-api-using-message-handlers. public interface IPrincipalProvider { IPrincipal GetPrincipal(string username,string password); } public sealed class Credentials { public string Username { get; set; } public string Password { get; set; } } public class BasicAuthMessageHandler : DelegatingHandler { private const string BasicAuthResponseHeader = "WWW-Authenticate"; private const string BasicAuthResponseHeaderValue = "Basic"; public IPrincipalProvider PrincipalProvider { get; private set; } public BasicAuthMessageHandler(IPrincipalProvider provider) { if (provider == null) throw new ArgumentNullException("provider"); PrincipalProvider = provider; } private static Credentials ParseAuthorizationHeader(string sHeader) { string[] credentials = Encoding.ASCII.GetString( Convert.FromBase64String(sHeader)).Split(new[] { ':' }); if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0]) || String.IsNullOrEmpty(credentials[1])) return null; return new Credentials { Username = credentials[0],Password = credentials[1],}; } protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync( HttpRequestMessage request,CancellationToken cancellationToken) { AuthenticationHeaderValue authValue = request.Headers.Authorization; if (authValue != null && !String.IsNullOrWhiteSpace(authValue.Parameter)) { Credentials parsedCredentials = ParseAuthorizationHeader(authValue.Parameter); if (parsedCredentials != null) { Thread.CurrentPrincipal = PrincipalProvider .GetPrincipal(parsedCredentials.Username,parsedCredentials.Password); } } return base.SendAsync(request,cancellationToken) .ContinueWith(task => { var response = task.Result; if (response.StatusCode == HttpStatusCode.Unauthorized && !response.Headers.Contains(BasicAuthResponseHeader)) { response.Headers.Add(BasicAuthResponseHeader,BasicAuthResponseHeaderValue); } return response; }); } } 解决方法
Here is another solution that meets all of your requirements.它使用SimpleMemberhsip,在MVC 4应用程序中混合使用表单身份验证和基本身份验证.它也可以支持授权,但不要将Role属性保留为null.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- ASP.NET Cookieless Forms在登录页面被加入书签时不设置coo
- asp.net – Orchard CMS 1.x [文档存储] – 混合记录支持和
- 在经典ASP调试的Visual Studio 2012中禁用了应用程序调试
- ASP.Net相当于PHP的file_get_contents(‘php:// input’);
- asp.net – 使用httpModule手动启用压缩
- asp.net-mvc – Asp.Net MVC CSLA DDD可能
- .NET 5 Preview 1的深度解读和跟进
- asp.net – 在VS 2012中使用Web Deploy指定不同的appSettin
- asp.net-mvc-4 – 为什么一些Asp.Net MVC4模板需要System.S
- 一个高效反射类
- 使用嵌套类的ASP.NET MVC3 JSON模型绑定
- Aspect-Oriented Programming : Aspect-Oriented
- asp.net – MVP模式和会话值
- asp.net-mvc – 使用ui-router的.NET MVC和Angul
- asp.net-mvc-4 – 当连接connecttring configSou
- asp.net-mvc – 同时发送文件并重定向到新页面的
- asp.net-mvc – 在构建时尝试构建ASP MVC视图时出
- 将.net核心连接到SSAS
- asp.net-mvc – 为什么在Asp.Net Identity 2.0 P
- asp.net – 基于角色为网站实现不同视图的最佳方