ASP.NET Web API 通过Authentication特性来实现身份认证
using System;
using System.Collections.Generic;
using System.Net.Http.Headers;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Filters;
using System.Web.Http.Results; namespace WebApi
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AuthenticateAttribute : FilterAttribute, IAuthenticationFilter
{
private static readonly Dictionary<string, string> UserAccounts; static AuthenticateAttribute()
{
UserAccounts = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
{"Foo", "Password"},
{"Bar", "Password"},
{"Baz", "Password"}
};
} public Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
IPrincipal user = null;
var headerValue = context.Request.Headers.Authorization;
if (null != headerValue && headerValue.Scheme == "Basic")
{
var credential = Encoding.Default.GetString(Convert.FromBase64String(headerValue.Parameter));
var split = credential.Split(':');
if (split.Length == )
{
var userName = split[];
string password;
if (UserAccounts.TryGetValue(userName, out password))
{
if (password == split[])
{
var identity = new GenericIdentity(userName);
user = new GenericPrincipal(identity, new string[]);
}
}
}
}
context.Principal = user;
return Task.FromResult<object>(null);
} public Task ChallengeAsync(HttpAuthenticationChallengeContext context, CancellationToken cancellationToken)
{
var user = context.ActionContext.ControllerContext.RequestContext.Principal;
if (null != user && user.Identity.IsAuthenticated) return Task.FromResult<object>(null);
var parameter = $"realm={context.Request.RequestUri.DnsSafeHost}";
var challenge = new AuthenticationHeaderValue("Basic", parameter);
context.Result = new UnauthorizedResult(new[] {challenge}, context.Request);
return Task.FromResult<object>(null);
}
}
}
ASP.NET Web API 通过Authentication特性来实现身份认证的更多相关文章
- (转)【ASP.NET Web API】Authentication with OWIN
概述 本文说明了如何使用 OWIN 来实现 ASP.NET Web API 的验证功能,以及在客户端与服务器的交互过程中,避免重复提交用户名和密码的机制. 客户端可以分为两类: JavaScript: ...
- ASP.NET Web API Basic Identity 中的基本身份验证
缺点 用户凭证在请求中发送. 凭据作为明文发送. 每个请求都会发送凭据. 无法注销,除非结束浏览器会话. 易于跨站点请求伪造(CSRF); 需要反CSRF措施. 优点 互联网标准. 受所有主要浏览器支 ...
- ASP.NET Web API 实现客户端Basic(基本)认证 之简单实现
优点是逻辑简单明了.设置简单. 缺点显而易见,即使是BASE64后也是可见的明文,很容易被破解.非法利用,使用HTTPS是一个解决方案. 还有就是HTTP是无状态的,同一客户端每次都需要验证. 实现: ...
- asp.net Web API简单的特性路由配置
一,控制器代码: [RoutePrefix("ajilisiwei")] //加URL前缀 (非必需) public class ShoppingController : ...
- ASP.NET Web API 特性
ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...
- ASP.NET Web API 简介
ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...
- ASP.NET Web API安全认证
http://www.cnblogs.com/codeon/p/6123863.html http://open.taobao.com/docs/doc.htm?spm=a219a.7629140.0 ...
- [转]ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
本文转自:http://www.cnblogs.com/parry/p/ASPNET_MVC_Web_API_digest_authentication.html 在前一篇文章中,主要讨论了使用HTT ...
- ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)
在前一篇文章中,主要讨论了使用HTTP基本认证的方法,因为HTTP基本认证的方式决定了它在安全性方面存在很大的问题,所以接下来看看另一种验证的方式:digest authentication,即摘要认 ...
随机推荐
- xml和xsd架构文档相关知识
1.使用架构(XSD)验证XML文件 2.使用自动生成工具: 工具目录:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4. ...
- java匿名类
一般情况下,我们需要声明一个类去继承一个接口,然后再new这个类,赋值给接口.但有时后这个类只会被调用一次,为了调用方便,那么就可以用匿名类来简化这个步骤. interface IKey{ void ...
- linux中mysql密码找回的两种方式
方法一:修改my.cnf配置文件 1.首先确认服务器出于安全的状态,也就是没有人能够任意地连接MySQL数据库. 因为在重新设置MySQL的root密码的期间,MySQL数据库完全出于没有密码保护的 ...
- [原]如何在Android用FFmpeg+SDL2.0解码显示图像
如何在Android上使用FFmpeg解码图像参考文章[原]如何在Android用FFmpeg解码图像 ,如何在Android上使用SDL2.0来显示图像参考[原]零基础学习SDL开发之在Androi ...
- access里like的通配符不能用%,要用*
转自http://www.knowsky.com/339881.html access里like的通配符用法是这样: “?”表示任何单一字符: “*”表示零个或多个字符: “#”表示任何一个数 ...
- SQL数据类型解释
SQL数据类型解释 1.char.varchar.text.ntext.bigint.int.smallint.tinyint和bit的区别及数据库的数据类型电脑秘籍 2009-05-15 21:47 ...
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- 一款 .NET 下的轻量级 REST 和 HTTP API 客户端 - RestSharp
项目地址:https://github.com/restsharp/RestSharp Features Supports .NET 3.5+, Silverlight 4, Windows Phon ...
- WPF中多个RadioButton绑定到一个属性
如图样: 在View中: <RadioButton IsChecked="{Binding Option, Converter={cvt:EnumToBooleanConverter} ...
- Shader的语法
Shader "name" { [Properties] Subshaders [Fallback] }(1)Properties:{ Property [Property ... ...