asp.net-mvc-4 – 使用基本HTTP身份验证的MVC4 app WebApi中的SimpleMembersh
|
我正在尝试实现具有以下要求的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 – context.Response.Charset = Encoding.UTF8.ToS
- asp.net – 动态创建的DropDownList在Postback上丢失了List
- asp.net – 创建新的实体数据模型的麻烦
- asp.net-mvc – 在没有模型的情况下手动将验证添加到文本框
- asp.net-mvc – “信号量超时期限已过期”SQL Azure
- 如果ASP.NET应用程序的CustomErrors设置为Off,有没有办法以
- ASP.NET MVC是否使Web表单成为旧版平台?
- asp.net-mvc – MVC自定义ViewModel和自动绑定
- asp.net-mvc-4 – .NET 4.5中没有调用HttpModule
- asp.net-mvc – EntityFramework – 连接字符串在哪里?
- asp.net – 在我的网站中添加HttpModule时出现“
- asp.net – 脚本标签和链接标签进入asp:内容或外
- asp.net – jqgrid editurl:控制器动作参数
- asp.net-mvc – 为什么在MVC中传递实体不是一个好
- asp.net-mvc-4 – GoogleOauth2问题获取Internal
- asp.net-mvc-3 – ASP.NET MVC3 Razor:没有@if或
- asp.net-core – 为什么我的JWT承载认证在令牌表
- asp.net-web-api – 不能构造String类型
- asp.net-mvc-5 – WebApi 2和MVC 5用户使用不同的
- asp.net – IItemTransform和现有的缩小文件
