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特性来实现身份认证的更多相关文章

  1. (转)【ASP.NET Web API】Authentication with OWIN

    概述 本文说明了如何使用 OWIN 来实现 ASP.NET Web API 的验证功能,以及在客户端与服务器的交互过程中,避免重复提交用户名和密码的机制. 客户端可以分为两类: JavaScript: ...

  2. ASP.NET Web API Basic Identity 中的基本身份验证

    缺点 用户凭证在请求中发送. 凭据作为明文发送. 每个请求都会发送凭据. 无法注销,除非结束浏览器会话. 易于跨站点请求伪造(CSRF); 需要反CSRF措施. 优点 互联网标准. 受所有主要浏览器支 ...

  3. ASP.NET Web API 实现客户端Basic(基本)认证 之简单实现

    优点是逻辑简单明了.设置简单. 缺点显而易见,即使是BASE64后也是可见的明文,很容易被破解.非法利用,使用HTTPS是一个解决方案. 还有就是HTTP是无状态的,同一客户端每次都需要验证. 实现: ...

  4. asp.net Web API简单的特性路由配置

    一,控制器代码: [RoutePrefix("ajilisiwei")]  //加URL前缀 (非必需)    public class ShoppingController : ...

  5. ASP.NET Web API 特性

    ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...

  6. ASP.NET Web API 简介

    ASP.NET MVC 4 包含了 ASP.NET Web API, 这是一个创建可以连接包括浏览器.移动设备等多种客户端的 Http 服务的新框架, ASP.NET Web API 也是构建 RES ...

  7. ASP.NET Web API安全认证

    http://www.cnblogs.com/codeon/p/6123863.html http://open.taobao.com/docs/doc.htm?spm=a219a.7629140.0 ...

  8. [转]ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)

    本文转自:http://www.cnblogs.com/parry/p/ASPNET_MVC_Web_API_digest_authentication.html 在前一篇文章中,主要讨论了使用HTT ...

  9. ASP.NET Web API(三):安全验证之使用摘要认证(digest authentication)

    在前一篇文章中,主要讨论了使用HTTP基本认证的方法,因为HTTP基本认证的方式决定了它在安全性方面存在很大的问题,所以接下来看看另一种验证的方式:digest authentication,即摘要认 ...

随机推荐

  1. C_中使用SendMessage

    SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...

  2. saiku源代码安装

    以前的文章介绍了如何直接安装saiku,http://www.cnblogs.com/liqiu/p/5183894.html .这里面偷懒没有源代码编译,不过这几天也就这么用了. 最近随着使用的深入 ...

  3. 未知高度定宽div水平居中及垂直居中(兼容ie6及其他牛逼浏览器)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. SQLServer创建维护计划失败 错误c001f011

    重新注册dts.dll文件,在运行里输入命令:(x64)regsvr32 "C:\Program Files\Microsoft SQL Server\100\DTS\Binn\dts.dl ...

  5. IL速查

    名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. Add.Ovf.Un 将两个无符号整数值相加,执行溢出检查,并且 ...

  6. Android开发艺术探索笔记——View(二)

    Android开发艺术探索笔记--View(二) View的事件分发机制 学习资料: 1.Understanding Android Input Touch Events System Framewo ...

  7. ReentrantLock的使用

    class BoundedBuffer { final Lock lock = new ReentrantLock(); final Condition notFull = lock.newCondi ...

  8. 图文详解远程部署ASP.NET MVC 5项目 [转载]

    话外篇: 由于感觉自己的机器比较慢,配置不好,所以最近想把之前的项目部署到实验室的服务器上,但是由于常不在实验室,所以在想能不能远程部署.因此今天专门研究了一下具体的过程,下面和大家分享一下.本人新手 ...

  9. 从头学Qt Quick(2)-- QML语法从一个简单的例子说起

    在上一篇文章中,我们对QtQuick做了简单的介绍,体验了使用QML语言构建一个UI的便捷.这里我们简要介绍一下QML的语法. QML将界面分成一些更小的元素,这些元素可以组成一个组件,QML语言描述 ...

  10. Could not load file or assembly 'System.Core, Version=2.0.5.0 和autofac冲突的问题

    在部署到iis的时候会出现这个状况. 解决:下载安装这个补丁 http://support.microsoft.com/kb/2468871 http://www.microsoft.com/zh-c ...