场景:你自己实现了一套webApi,想供自己的客户端调用,又想做认证。

第一步:通过vs2015建立web api项目,Startup.cs,这个类将会作为Owin的启动类。

第二步:在webapi.config中添加如下代码:

第三步:在web.config中添加连接字符串(这里使用了EF Code First)

第四步:添加上下文对象

第五步:添加AuthRepository类,增加用户注册和查找功能:

第六步:增加AccountController(

using Microsoft.AspNet.Identity;
using Owin2.Auth;
using Owin2.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http; namespace Owin2.Controllers
{
[RoutePrefix("api/Account")]
public class AccountController : ApiController
{
private readonly AuthRepository _authRepository = null; public AccountController()
{
_authRepository = new AuthRepository();
}
// POST api/Account/Register Register方法打上了AllowAnonymous标签,意味着调用这个api无需任何授权。
[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(UserModel userModel)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
} IdentityResult result = await _authRepository.RegisterUser(userModel); IHttpActionResult errorResult = GetErrorResult(result); if (errorResult != null)
{
return errorResult;
} return Ok();
} protected override void Dispose(bool disposing)
{
if (disposing)
{
_authRepository.Dispose();
} base.Dispose(disposing);
} private IHttpActionResult GetErrorResult(IdentityResult result)
{
if (result == null)
{
return InternalServerError();
} if (!result.Succeeded)
{
if (result.Errors != null)
{
foreach (string error in result.Errors)
{
ModelState.AddModelError("", error);
}
} if (ModelState.IsValid)
{ // No ModelState errors are available to send, so just return an empty BadRequest.
return BadRequest();
} return BadRequest(ModelState);
} return null;
}
}
}

  

认证策略的类:

using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OAuth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web; namespace Owin2.Auth
{
public class SimpleAuthorizationServerProvider: OAuthAuthorizationServerProvider
{ /// <summary>
/// ValidateClientAuthentication方法用来对third party application 认证,
/// 具体的做法是为third party application颁发appKey和appSecrect,
/// 在本例中我们省略了颁发appKey和appSecrect的环节,
/// 我们认为所有的third party application都是合法的,context.Validated();
/// 表示所有允许此third party application请求。
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
return Task.FromResult<object>(null);
} /// <summary>
/// GrantResourceOwnerCredentials方法则
/// 是resource owner password credentials模式的重点
/// 由于客户端发送了用户的用户名和密码,
/// 所以我们在这里验证用户名和密码是否正确,
/// 后面的代码采用了ClaimsIdentity认证方式,
/// 其实我们可以把他当作一个NameValueCollection看待。
/// 最后context.Validated(ticket); 表明认证通过
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
using (AuthRepository _repo = new AuthRepository())
{
IdentityUser user = await _repo.FindUser(context.UserName, context.Password); if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
} var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
identity.AddClaim(new Claim(ClaimTypes.Role, "user"));
identity.AddClaim(new Claim("sub", context.UserName)); var props = new AuthenticationProperties(new Dictionary<string, string>
{
{
"as:client_id",context.ClientId ?? string.Empty
},
{
"userName",context.UserName
}
}); var ticket = new AuthenticationTicket(identity, props);
context.Validated(ticket);
} /// <summary>
/// TokenEndpoint方法将会把Context中的属性加入到token中。
/// </summary>
/// <param name="context"></param>
/// <returns></returns>
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
{
context.AdditionalResponseParameters.Add(property.Key, property.Value);
} return Task.FromResult<object>(null);
} }
}

  

OAuth2服务已经搭建好了,建立客户端来测试。

第七步:建立客户端(新建另外一个api程序),并且配置Startup.cs

第八步:配置web api.config

第九步:建立测试api控制器

1.先注册

2.通过用户名和密码获取token

3.通过token请求api

客户端和服务端通过配置文件关联。

OAuth2建立webapi认证服务供自己的客户端使用--密码模式的更多相关文章

  1. 使用OAuth打造webapi认证服务供自己的客户端使用

    一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前的版本是2.0版.注意是Authorization(授权),而不是Authentication(认证). ...

  2. 使用OAuth打造webapi认证服务供自己的客户端使用(二)

    在上一篇”使用OAuth打造webapi认证服务供自己的客户端使用“的文章中我们实现了一个采用了OAuth流程3-密码模式(resource owner password credentials)的W ...

  3. OAuth打造webapi认证服务

    使用OAuth打造webapi认证服务供自己的客户端使用 一.什么是OAuth OAuth是一个关于授权(Authorization)的开放网络标准,目前的版本是2.0版.注意是Authorizati ...

  4. Spring Boot Security Oauth2之客户端模式及密码模式实现

    Spring Boot Security Oauth2之客户端模式及密码模式实现 示例主要内容 1.多认证模式(密码模式.客户端模式) 2.token存到redis支持 3.资源保护 4.密码模式用户 ...

  5. SpringBootSecurity学习(17)前后端分离版之 OAuth2.0 数据库(JDBC)存储客户端

    自动批准授权码 前面我们授权的流程中,第一步获取授权码的时候,都会经历一个授权是否同意页面: 这个流程就像第三方登录成功后,提问是否允许获取昵称和头像信息的页面一样,这个过程其实是可以自动同意的,需要 ...

  6. Core篇——初探IdentityServer4(客户端模式,密码模式)

    Core篇——初探IdentityServer4(客户端模式,密码模式) 目录 1.Oatuth2协议的客户端模式介绍2.IdentityServer4客户端模式实现3.Oatuth2协议的密码模式介 ...

  7. postgres(pgAdmin) 客户端保存密码

    pgAdmin 大象客户端保存密码后连接服务器,删除掉当前连接,建立一个新的连接不用输入密码也能连接上,其实是客户端保存了密码,让人误以为是空密码可登录.可以通过右键连接,选择重载服务配置,再次连接就 ...

  8. 在线建立或重做mysql主从复制架构方法(传统模式和GTID模式)【转】

    mysql主从复制架构,是mysql数据库主要特色之一,绝大多数公司都有用到. 而GTID模式是基于事务的复制模式的意思,发展到现在也是越来越多人用. 以前很多文章,介绍搭建mysql主从复制架构,是 ...

  9. Security-OAuth2.0 密码模式之客户端实现

    我的OAuth2.0 客户端项目目录 pom 的配置 <?xml version="1.0" encoding="UTF-8"?> <proj ...

随机推荐

  1. vue-cli 结构

    . |-- build                            // 项目构建(webpack)相关代码 |   |-- build.js                     // ...

  2. wp版笔记本应用源码

    今天在那个WP教程网看到了一个不错的项目,简单的记事本,主要是用到的独立存储文件的操作,TimePicker和DatePicker的是用,数据绑定,界面的参考的chanraycode的,主要是锻炼自己 ...

  3. SPL类

    用途:对类,方法,属性,参数的提取生成文档:自动加载插件 实列化类同于new:$ref = new ReflectionClass($classname);$class = $ref->newI ...

  4. IE 11 浏览器兼容性视图设置

    1.打开IE 浏览器. 2.选择“工具”---“兼容性视图设置”.3.在“在兼容性视图中显示所有网站”前面勾选住. 点击关闭就可以了. 开发人员工具 1.找到“工具”----“F12开发人员工具”.2 ...

  5. qqbot 出现请求接口失败的问题

    解决方法: 找到python安装目录下“Lib\site-packages\qqbot\qcontactdb\fetch.py”文件下的“http://s.web2.qq.com” 替换成 “http ...

  6. [luogu3627 APIO2009] 抢掠计划 (tarjan缩点+spfa最长路)

    传送门 Description Input 第一行包含两个整数 N.M.N 表示路口的个数,M 表示道路条数.接下来 M 行,每行两个整数,这两个整数都在 1 到 N 之间,第 i+1 行的两个整数表 ...

  7. python简单post信息

    最近学了点关于python的网络爬虫的知识,简单记录一下,这里主要用到了requests库和BeautifulSoup库 Requests is an elegant and simple HTTP ...

  8. POJ 4786 Fibonacci Tree

    Fibonacci Tree Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ...

  9. nyoj 803 大数问题

    #include<stdio.h> #include<string.h> #define ll long long #define N 110000 int main() { ...

  10. 编译打包部署 Dubbo Admin

    1.下载,Dubbo地址: https://github.com/alibaba/dubbo/tree/2.5.x ,直接ZIP下载 2.解压并打开项目,mvn package 得到war包,如下图: ...