Decrypting OWIN Authentication Ticket
参考:https://long2know.com/2015/05/decrypting-owin-authentication-ticket/
AuthServer产生的Token因为没有制定自定义的加密逻辑,所以会使用默认的加密算法,故只能被AuthServer自身解密。
所以下列代码必须写在AuthServer项目内部才能使用。
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.DataHandler;
using Microsoft.Owin.Security.DataProtection;
using Microsoft.Owin.Security.OAuth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Web.Http;
using System.Web.Http.Results;
using System.IdentityModel.Tokens;
using Microsoft.Owin.Security.Jwt; namespace DIH.Core.AuthServer.IIS
{
[RoutePrefix("api/my")]
public class MyController : ApiController
{
public MyController()
{
} [Route("", Name = "DecryptToken")]
[HttpPost]
public IHttpActionResult DecryptToken([FromBody]string token)
{
token = "3l4Bg-xYshdFlaD4In_RZLoDUyx-BcMyVafx97WMPrm59hyQzovjbANjCQ6Yaz6C9OnYSoGy5WvrB79lKdncUIEcxACFrdTGFzTlyTqPOrwm7HwpCa-zTPVnk3jBgq72joub58FPKxQozdyN0JqvIgB6MyRX9GfVukS2tGQltEQHCJGJDmRYfcUo0l4YTgomA9zYWIE_ERryYkeXL1zN0WKHX_QrYTADRaPKcniZ-iMoZ7v9i5vSV_GFGdDJ4BYS";
var secureDataFormat = new TicketDataFormat(new MachineKeyProtector());
AuthenticationTicket ticket = secureDataFormat.Unprotect(token); string AuthenticationType = ticket.Identity.AuthenticationType;
List<Claim> lstClaim = ticket.Identity.Claims.Select(claim => claim).ToList(); var a = new Microsoft.Owin.Security.Jwt.JwtFormat(new TokenValidationParameters()
{ });
string jwt = a.Protect(ticket); return Ok(jwt);
}
} /// <summary>
/// Helper method to decrypt the OWIN ticket
/// </summary>
class MachineKeyProtector : IDataProtector
{
private readonly string[] _purpose = new string[]
{
typeof(OAuthAuthorizationServerMiddleware).Namespace,
"Access_Token",
"v1"
};
public byte[] Protect(byte[] userData)
{
//throw new NotImplementedException();
return System.Web.Security.MachineKey.Protect(userData, _purpose);
} public byte[] Unprotect(byte[] protectedData)
{
return System.Web.Security.MachineKey.Unprotect(protectedData, _purpose);
}
} }
Decrypting OWIN Authentication Ticket的更多相关文章
- Asp.Net WebApi 使用OWIN架构后,出现 “没有 OWIN 身份验证管理器与此请求相关联(No OWIN authentication manager is associated with the request)” 异常的解决办法
在Asp.Net WebApi 项目中使用OWIN模块之后,如果没有在OWIN的Startup类中配置认证方式,调用WebApi的相关Controller和Action就会出现如下异常: 出现错误. ...
- 基于OWIN WebAPI 使用OAuth授权服务【客户端验证授权(Resource Owner Password Credentials Grant)】
适用范围 前面介绍了Client Credentials Grant ,只适合客户端的模式来使用,不涉及用户相关.而Resource Owner Password Credentials Grant模 ...
- [转] JSON Web Token in ASP.NET Web API 2 using Owin
本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...
- JSON Web Token in ASP.NET Web API 2 using Owin
In the previous post Decouple OWIN Authorization Server from Resource Server we saw how we can separ ...
- Implement JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1 Part 3 (by TAISEER)
http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-an ...
- MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN
在Membership系列的最后一篇引入了ASP.NET Identity,看到大家对它还是挺感兴趣的,于是来一篇详解登录原理的文章.本文会涉及到Claims-based(基于声明)的认证,我们会详细 ...
- [转]MVC5 - ASP.NET Identity登录原理 - Claims-based认证和OWIN
本文转自:http://www.cnblogs.com/jesse2013/p/aspnet-identity-claims-based-authentication-and-owin.html 在M ...
- [转]Web APi之认证(Authentication)及授权(Authorization)【一】(十二)
本文转自:http://www.cnblogs.com/CreateMyself/p/4856133.html 前言 无论是ASP.NET MVC还是Web API框架,在从请求到响应这一过程中对于请 ...
- Web APi之认证(Authentication)及授权(Authorization)【一】(十二)
前言 无论是ASP.NET MVC还是Web API框架,在从请求到响应这一过程中对于请求信息的认证以及认证成功过后对于访问页面的授权是极其重要的,用两节来重点来讲述这二者,这一节首先讲述一下关于这二 ...
随机推荐
- Java中的反射[转载]
转自:https://blog.csdn.net/sinat_38259539/article/details/71799078#commentBox 1.什么是反射? 反射是通过一个类可以知道其中所 ...
- Oracle TRCA 工具(转)
本篇文章主要介绍了"Oracle TRCA 工具 说明 ",主要涉及到Oracle TRCA 工具 说明 方面的内容,对于Oracle TRCA 工具 说明 感兴趣的同学可以参考一 ...
- python select poll
http://www.cnblogs.com/coser/archive/2012/01/06/2315216.html
- 核心动画(CAKeyframeAnimation,CABasicAnimation)
一,核心动画常用的三种例子 view的核心动画其体现就是把view按照指定好的路径进行运动,针对的是view的整体. [view.layer addAnimation:动画路径 forKey:@“绑定 ...
- MySQL数据库--练习
学生选课系统 设计表关系 创建表和插入数据 /* Navicat MySQL Data Transfer Source Server : localhost_3306 Source Server Ve ...
- XDU 1056
解法一:简单搜索肯定TLE,只是单纯的想写一发搜索练练手 #include<cstdio> #include<cstring> #define maxn 1005 using ...
- ruby中的可调用对象--方法
上一篇讲了ruby中的可调用对象proc和lambda,他们都是块转换成的对象.ruby中的可调用对象还有方法.通过使用method方法,并且以方法名作为参数(字符串或者符号),就可以得到一个方法对象 ...
- Django restful Framework 之Requests and Response 方法
前言: 本章主要介绍REST framework 内置的必要的功能. Request objects Response objects Status codes Wrapping API views ...
- Spring-1-E Game(HDU 5011)解题报告及测试数据
Game Time Limit:1000MS Memory Limit:65536KB Description Here is a game for two players. The rule ...
- windows平台kettle连接hbase的问题
我本机安装的环境是centos7,并在本机上安装了zookeeper,hadoop,hbase,hive等组件, 使用pdi7.1来连接hbase,把mysql表中的数据导出到hbase中去,没有问题 ...