.NET 请求认证之access-token(oauth2.0与owin的结合)

      公司对外开放的接口,大多须要请求认证,如微信,阿里等。最近我恰好在搭建webapi框架。记录一下本身的理解。web

一:请求认证的目的api

  之因此要对咱们的接口进行请求认证,就是为了安全考虑的。之前都是在用别人给个人规则去生成token,如今也轮到本身开发了。嘿嘿浏览器

二:开发思路安全

  1:请求接口以前,用户必须先去请求得到咱们的access-token。每次请求必须得带上access-token来请求个人接口。不然咱们会拒绝外部请求。 微信

  2:access-token有时间的限制,过时的请求咱们依然会拒绝。app

  3:对不一样的外部者,应有不一样的clientID。以便分配不一样的权限。之后不合做了,咱们能够取消他们的认证,并不会影响其余的客户。相似于微信的 Appid,Appsecret框架

三:建立Access-Tokenasync

      在api项目里token验证发生在进入接口以前,须要有一个启动文件来执行token的判断。在API项目下建立Startup.cs类。ide

     咱们须要在Nuget引用如下DLL:ui

  • Microsoft.Owin.Security.OAuth
  • Microsoft.Owin.Security
  • Microsoft.Owin
  • Microsoft.Owin.Host.SystemWeb
  • OWIN
  • Microsoft ASP.Net Web API 2.2 OWIN
  • Microsoft ASP.Net Identity OWIN
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using Bn.Common;
using Microsoft.Owin;
using Microsoft.Owin.Cors;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OAuth;
using Owin;

[assembly: OwinStartup(typeof(BnWebApi.Startup))]

namespace BnWebApi
{
    /// <summary>
    /// wuchen19-4-15
    /// </summary>
    public partial class Startup
    {
        //public void Configuration(IAppBuilder app)
        //{
        //    ConfigureAuth(app);
        //}
        public void Configuration(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();
            WebApiConfig.Register(config);
            ConfigureOAuth(app);
            app.UseCors(CorsOptions.AllowAll);
            app.UseWebApi(config);
        }

        public void ConfigureOAuth(IAppBuilder app)
        {
            var OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                AllowInsecureHttp = true, //容许客户端使用Http协议请求
                TokenEndpointPath = new PathString("/token"), //请求地址
                //AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), //token过时时间
                AccessTokenExpireTimeSpan = TimeSpan.FromHours(1),
                Provider = new SimpleAuthorizationServerProvider(),//提供认证策略
                RefreshTokenProvider = new SimpleRefreshTokenProvider() //refresh_token 受权服务
            };
            app.UseOAuthAuthorizationServer(OAuthServerOptions);
            app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
        }




    }
}

这个SimpleRefreshTokenProvider()方法就是咱们验证Client信息和生成Token的地方

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.OAuth;
using  System.Configuration;

namespace Bn.Common
{
    /// <summary>
    /// Token验证    wuchen  5-17
    /// </summary>
    public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
    {


        #region  AccessToken生成机制   密码模式   
        ////Oauth AccessToken  grant_type=password

        //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[] { "*" }));
        //    var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        //    var bnauth = ConfigurationManager.AppSettings["Oauth"];  //BnUser-123456
        //    var bnstr = bnauth.Split('-');
        //    var bnuser = bnstr[0];
        //    var bnpassword = bnstr[1];
        //    if (context.UserName != bnuser || context.Password != bnpassword)
        //    {
        //        context.SetError("invalid_client", "帐号密码认证失败");
        //         return;
        //    }
        //    identity.AddClaim(new Claim("sub", context.UserName));
        //    identity.AddClaim(new Claim("role", "user"));
        //    context.Validated(identity);
        //}
        #endregion



        #region  AccessToken生成机制   客户端模式     
        // grant_type= client_credentials
        public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
        {
            string clientId;
            string clientSecret;
            context.TryGetFormCredentials(out clientId, out clientSecret);
            //验证
            //var bnauth = ConfigurationManager.AppSettings["Oauth"];  //BnUser-123456
            //var bnstr = bnauth.Split('-');
            //var bnuser = bnstr[0];
            //var bnpassword = bnstr[1];
            //if (clientId != bnuser || clientSecret != bnpassword)
            //{
            //    context.SetError("invalid_client", "帐号密码认证失败");
            //    return;
            //}

            var gkey = ConfigurationManager.AppSettings[clientId];  //BnUser-123456
            if (clientSecret != gkey)
            {
                context.SetError("invalid_client", "帐号密码认证失败");
                return;
            }

            context.OwinContext.Set("as:client_id", clientId);
            await Task.Factory.StartNew(() => context.Validated(clientId));
            //context.Validated(clientId);
        }
        /// <summary>
        ///     客户端受权[生成access token]
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public override Task GrantClientCredentials(OAuthGrantClientCredentialsContext context)
        {
            var oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
            oAuthIdentity.AddClaim(new Claim(ClaimTypes.Name, context.OwinContext.Get<string>("as:client_id")));
            var ticket = new AuthenticationTicket(oAuthIdentity, new AuthenticationProperties { AllowRefresh = true });
            context.Validated(ticket);
            return base.GrantClientCredentials(context);
        }
        #endregion

    }
}

两种模式都是能够用的。。密码模式和客户端模式。GrantClientCredentials方法为咱们生成Token的方法。。有了这些咱们就能够生成token啦。

Oauth支持的5类 grant_type 及说明(
authorization_code — 受权码模式(即先登陆获取code,再获取token)
password — 密码模式(将用户名,密码传过去,直接获取token)
client_credentials — 客户端模式(无用户,用户向客户端注册,而后客户端以本身的名义向’服务端’获取资源)
implicit — 简化模式(在redirect_uri 的Hash传递token; Auth客户端运行在浏览器中,如JS,Flash)
refresh_token — 刷新access_token) 

四:获取Token

输入参数:grant_type:验证模式 ,   client_id :服务端定义 ,  client_secret:秘钥 服务端和客户端共同保存   相似于微信的Appsecrret

生成项目,而后本地用Postman调试

返回的参数分别是:access_token:咱们须要的token        token_type :bearer  (协议固定)         expires_in:有效期(能够自定义)        refresh_token:token从新过时后,从新获取token用到(暂时没用)

  五:使用access_token访问接口路由

 在Webapi中 ,咱们用在方法加上 Authorize ,来表示这个方法访问须要受权, 若是不加Authorize ,那么token就没有意义。。  以下

 

咱们不带token访问一下看看:

 

 显示已拒绝咱们的请求受权。

 加了access-Token以后,注意token放的位置,在请求头Headers里  添加 Authorization:bearer token。      bearer与token之间有一个空格

验证的过程,是在咱们Oauth2.0封装起来啦,能够查看。

到这里,用Oauth2.0加Owin的Webapi请求验证--accesstoken完成。。基本每一步都有注释,很好理解

     

      简单的两句代码,双倍的快乐

相关文章
相关标签/搜索