owinAuthorize
Nuget包获取
- Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.1.2
- Install-Package Microsoft.Owin.Host.SystemWeb -Version 2.1.0
- Install-Package Microsoft.AspNet.Identity.Owin -Version 2.0.1
- Install-Package Microsoft.Owin.Cors -Version 2.1.0
- Install-Package EntityFramework -Version 6.0.0
添加OwinStartUp类
- using System;
- using System.Web.Http;
- using Owin;
- using Microsoft.Owin;
- using Microsoft.Owin.Security.OAuth;
- using SqlSugar.WebApi;
- [assembly: OwinStartup(typeof(WebApi.Startup))]
- namespace WebApi
- {
- public class Startup
- {
- public void Configuration(IAppBuilder app)
- {
- HttpConfiguration config = new HttpConfiguration();
- ConfigureOAuth(app);
- WebApiConfig.Register(config);
- app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
- app.UseWebApi(config);
- }
- public void ConfigureOAuth(IAppBuilder app)
- {
- OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
- {
- AllowInsecureHttp = true,
- TokenEndpointPath = new PathString("/token"),
- AccessTokenExpireTimeSpan = TimeSpan.FromDays(),
- Provider = new SimpleAuthorizationServerProvider()
- };
- app.UseOAuthAuthorizationServer(OAuthServerOptions);
- app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
- }
- }
- }
添加验证类SimpleAuthorizationServerProvider
- using System.Threading.Tasks;
- using System.Security.Claims;
- using Microsoft.Owin.Security.OAuth;
- namespace WebApi
- {
- /// <summary>
- /// Token验证
- /// </summary>
- public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
- {
- public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
- {
- await Task.Factory.StartNew(() => context.Validated());
- }
- public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
- {
- await Task.Factory.StartNew(() => context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }));
- /*
- * 对用户名、密码进行数据校验
- 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("sub", context.UserName));
- identity.AddClaim(new Claim("role", "user"));
- context.Validated(identity);
- }
- }
- }
添加验证
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- namespace WebApplication1.Controllers
- {
- [Authorize]
- public class ValuesController : ApiController
- {
- // GET api/values
- public IEnumerable<string> Get()
- {
- return new string[] { "value1", "value2" };
- }
- // GET api/values/5
- public string Get(int id)
- {
- return "value";
- }
- // POST api/values
- public void Post([FromBody]string value)
- {
- }
- // PUT api/values/5
- public void Put(int id, [FromBody]string value)
- {
- }
- // DELETE api/values/5
- public void Delete(int id)
- {
- }
- }
- }
获取token
调接口bearer Token
https://www.cnblogs.com/Hai--D/p/6187051.html
owinAuthorize的更多相关文章
随机推荐
- Java-Maven-Runoob:Maven 构建 & 项目测试
ylbtech-Java-Maven-Runoob:Maven 构建 & 项目测试 1.返回顶部 1. Maven 构建 & 项目测试 在上一章节中我们学会了如何使用 Maven 创建 ...
- cassandra学习 四 数据模型
Keyspace(建空间): 可以理解为Database: Replication factor: 复制因数 : Replica placement srategy: 复制策略,默认是Simple ...
- 在液晶屏里显示浮点数的方法 (sprintf 的妙用)
思路:使用 sprintf 函数将浮点型数据转为指定格式的字符串 #include <stdio.h> #include<string.h> int main() { unsi ...
- C# 面向切面编程--监控日志记录方案
背景:现在公司整体在做监控平台,要求把各个部分的细节都记录下来,在前台页面上有所显示,所以现在需要做的就是一个监控日志的记录工作,今天讲的就是渲染监控日志的例子. 现状:当前的渲染程序没有为监控日志记 ...
- flask ---映射到数据库
在当前项目文件下:运行cmd指令(terminal中) (1)python manage.py db init ----初始化文件 (2)python manage.py db migrate-- ...
- MIS系统部署方案
- DRF之REST规范介绍及View请求流程分析
编程是数据结构和算法的结合,而在Web类型的App中,我们对于数据的操作请求是通过url来承载的,本文详细介绍了REST规范和CBV请求流程. 编程是数据结构和算法的结合,小程序如简单的计算器,我们输 ...
- ORACLE各版本下载地址
ORACLE 10g下载|ORACLE 10g下载地址|ORACLE 10g官网下载地址 ORACLE 10g下载地址 oracle 下载还需要用户名我自己注册了个方便大家使用下载 user:1603 ...
- Python中正则表达式对中文的匹配问题
python匹配中文的时候特别要注意的是匹配的正则字符串是否是Unicode格式的: import re source = "s2f程序员杂志一2d3程序员杂志二2d3程序员杂志三2d3程序 ...
- 关于python3 发送邮件
一:发送文本信息 from email.mime.text import MIMEText from email.header import Header from smtplib import SM ...