Nuget包获取

  1. Install-Package Microsoft.AspNet.WebApi.Owin -Version 5.1.2
  2. Install-Package Microsoft.Owin.Host.SystemWeb -Version 2.1.0
  3. Install-Package Microsoft.AspNet.Identity.Owin -Version 2.0.1
  4. Install-Package Microsoft.Owin.Cors -Version 2.1.0
  5. Install-Package EntityFramework -Version 6.0.0

  添加OwinStartUp类

  1. using System;
  2. using System.Web.Http;
  3.  
  4. using Owin;
  5. using Microsoft.Owin;
  6. using Microsoft.Owin.Security.OAuth;
  7. using SqlSugar.WebApi;
  8.  
  9. [assembly: OwinStartup(typeof(WebApi.Startup))]
  10. namespace WebApi
  11. {
  12. public class Startup
  13. {
  14. public void Configuration(IAppBuilder app)
  15. {
  16. HttpConfiguration config = new HttpConfiguration();
  17. ConfigureOAuth(app);
  18.  
  19. WebApiConfig.Register(config);
  20. app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
  21. app.UseWebApi(config);
  22. }
  23.  
  24. public void ConfigureOAuth(IAppBuilder app)
  25. {
  26. OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
  27. {
  28. AllowInsecureHttp = true,
  29. TokenEndpointPath = new PathString("/token"),
  30. AccessTokenExpireTimeSpan = TimeSpan.FromDays(),
  31. Provider = new SimpleAuthorizationServerProvider()
  32. };
  33. app.UseOAuthAuthorizationServer(OAuthServerOptions);
  34. app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
  35. }
  36. }
  37. }

添加验证类SimpleAuthorizationServerProvider

  1. using System.Threading.Tasks;
  2. using System.Security.Claims;
  3. using Microsoft.Owin.Security.OAuth;
  4.  
  5. namespace WebApi
  6. {
  7. /// <summary>
  8. /// Token验证
  9. /// </summary>
  10. public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
  11. {
  12. public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
  13. {
  14. await Task.Factory.StartNew(() => context.Validated());
  15. }
  16.  
  17. public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
  18. {
  19. await Task.Factory.StartNew(() => context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }));
  20. /*
  21. * 对用户名、密码进行数据校验
  22. using (AuthRepository _repo = new AuthRepository())
  23. {
  24. IdentityUser user = await _repo.FindUser(context.UserName, context.Password);
  25.  
  26. if (user == null)
  27. {
  28. context.SetError("invalid_grant", "The user name or password is incorrect.");
  29. return;
  30. }
  31. }*/
  32.  
  33. var identity = new ClaimsIdentity(context.Options.AuthenticationType);
  34. identity.AddClaim(new Claim("sub", context.UserName));
  35. identity.AddClaim(new Claim("role", "user"));
  36.  
  37. context.Validated(identity);
  38.  
  39. }
  40. }
  41. }

添加验证

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Http;
  6. using System.Web.Http;
  7.  
  8. namespace WebApplication1.Controllers
  9. {
  10. [Authorize]
  11. public class ValuesController : ApiController
  12. {
  13. // GET api/values
  14. public IEnumerable<string> Get()
  15. {
  16. return new string[] { "value1", "value2" };
  17. }
  18.  
  19. // GET api/values/5
  20. public string Get(int id)
  21. {
  22. return "value";
  23. }
  24.  
  25. // POST api/values
  26. public void Post([FromBody]string value)
  27. {
  28. }
  29.  
  30. // PUT api/values/5
  31. public void Put(int id, [FromBody]string value)
  32. {
  33. }
  34.  
  35. // DELETE api/values/5
  36. public void Delete(int id)
  37. {
  38. }
  39. }
  40. }

获取token

调接口bearer Token

https://www.cnblogs.com/Hai--D/p/6187051.html

owinAuthorize的更多相关文章

随机推荐

  1. Java-Maven-Runoob:Maven 构建 & 项目测试

    ylbtech-Java-Maven-Runoob:Maven 构建 & 项目测试 1.返回顶部 1. Maven 构建 & 项目测试 在上一章节中我们学会了如何使用 Maven 创建 ...

  2. cassandra学习 四 数据模型

    Keyspace(建空间): 可以理解为Database: Replication factor: 复制因数 :   Replica placement srategy: 复制策略,默认是Simple ...

  3. 在液晶屏里显示浮点数的方法 (sprintf 的妙用)

    思路:使用 sprintf 函数将浮点型数据转为指定格式的字符串 #include <stdio.h> #include<string.h> int main() { unsi ...

  4. C# 面向切面编程--监控日志记录方案

    背景:现在公司整体在做监控平台,要求把各个部分的细节都记录下来,在前台页面上有所显示,所以现在需要做的就是一个监控日志的记录工作,今天讲的就是渲染监控日志的例子. 现状:当前的渲染程序没有为监控日志记 ...

  5. flask ---映射到数据库

    在当前项目文件下:运行cmd指令(terminal中)  (1)python manage.py db init ----初始化文件  (2)python manage.py db migrate-- ...

  6. MIS系统部署方案

  7. DRF之REST规范介绍及View请求流程分析

    编程是数据结构和算法的结合,而在Web类型的App中,我们对于数据的操作请求是通过url来承载的,本文详细介绍了REST规范和CBV请求流程. 编程是数据结构和算法的结合,小程序如简单的计算器,我们输 ...

  8. ORACLE各版本下载地址

    ORACLE 10g下载|ORACLE 10g下载地址|ORACLE 10g官网下载地址 ORACLE 10g下载地址 oracle 下载还需要用户名我自己注册了个方便大家使用下载 user:1603 ...

  9. Python中正则表达式对中文的匹配问题

    python匹配中文的时候特别要注意的是匹配的正则字符串是否是Unicode格式的: import re source = "s2f程序员杂志一2d3程序员杂志二2d3程序员杂志三2d3程序 ...

  10. 关于python3 发送邮件

    一:发送文本信息 from email.mime.text import MIMEText from email.header import Header from smtplib import SM ...