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的更多相关文章

随机推荐

  1. Java 数组的三种创建方法,数组拷贝方法

    public static void main(String[] args) {//创建数组的第一种方法int[] arr=new int[6];int intValue=arr[5];//Syste ...

  2. win10下默认使用福昕打开PDF

    win10为了推他的edge浏览器, 将默认的pdf打开设置为了edge浏览器, 非常令人反感, 做浏览器就好好做浏览器, 为什么要默认打开pdf? 而且修改默认为福昕后, 下次打开pdf文件, 他又 ...

  3. jQuery笔记——基础知识

    jQuery是一个JavaScript库,它通过封装原生的JavaScript函数得到一整套定义好的方法.在jQuery程序中,不管是页面元素的选择.内置的功能函数,都是美元符号“$”来起 始的.而这 ...

  4. 什么是语义化的HTML

    语义化的HTML使用每个html标签都特定的用途,例如p标签放大段文字, h1~h6常用于标题,strong加粗强掉….. 语义化的好处: 1:去掉或样式丢失的时候能让页面呈现清晰的结构: html本 ...

  5. JAVA事件监听机制的实现

    今天学习了java的事件编程机制,略有体会,先在此记下心得. 第一,首先明确几个概念. 事件源:一个产生或者触发事件的对象.事件:承载事件源状态改变时的信息对象.事件监听器接口:实际上就是一个类,该类 ...

  6. leetcode682

    class Solution { public: int calPoints(vector<string>& ops) { stack<int> ST; ; for ( ...

  7. 监控Mongo慢查询

    监控Mongo慢查询 1. 使用mongostat监控MongoDB全局情况 mongostat是mongdb自带的状态检测工具,在命令行下使用.它会间隔固定时间获取MongoDB的当前运行状态,并输 ...

  8. VS编译常见错误枚举01

    fatal error C1189: #error :  This file requires _WIN32_WINNT to be #defined at least to 0x0403. Valu ...

  9. coprime Sequence

    Do you know what is called ``Coprime Sequence''? That is a sequence consists of nn positive integers ...

  10. cuteFTP连接不上VM虚拟机中RedHat&amp;…

    摸索了一下午,终于解决了问题:主要原因是因为redhat系统配置文件默认root用户无法使用ftp,只需作如下修改就可以使用了.            1.找到/etc/vsftpd/目录修改下面的连 ...