如何使用owin和Mvc 5从httpcontext获取访问令牌

编程入门 行业动态 更新时间:2024-10-24 20:17:02
本文介绍了如何使用owin和Mvc 5从httpcontext获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经在 IdentityServer 4 中实现了IDP.我的Web应用程序客户端(在Mvc 5中实现)通过IDP进行身份验证,但现在我需要从请求中获取访问令牌. 在.Net Core中执行此操作的一种方法是使用 Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions 像这样:

I've got a IDP implemented in IdentityServer 4. My web app client(implemented in Mvc 5) authenticates with the IDP but now I need to get the access token from the request. A way to do that in .Net Core is to use the Microsoft.AspNetCore.Authentication.AuthenticationTokenExtensions like so:

HttpContext.Authentication.GetTokenAsync("acccess_token")

我希望能够在 Mvc5 Web应用程序客户端中执行相同的操作,但是找不到具有类似实现的任何nuget包或名称空间.能够在MVC5中而不是 Core中执行此操作很重要.有人遇到过这个吗?

I would like to be able to do the same in my Mvc5 web app client but I can't find any nuget package or namespace that has a similar implementation. It is important to be able to do this in MVC5 and not Core. Anyone came across this before?

PS-还值得一提的是,我正在使用OpenIdConnect

PS- Also worth to mention that I'm using OpenIdConnect

推荐答案

最近发布的 Katana的4.1.0版本现在支持 SaveTokens属性(从ASP.NET Core反向移植).

The recently released 4.1.0 version of Katana now supports the SaveTokens property (backported from ASP.NET Core).

为了获取访问令牌:

  • 更新 Microsoft.Owin.Security.OpenIdConnect 打包到4.1.0(或更高版本)
  • 在启动类中配置SaveTokens:
  • Update the Microsoft.Owin.Security.OpenIdConnect package to 4.1.0 (or newer)
  • Configure SaveTokens in your Startup class:
  • app.UseOpenIdConnectAuthentication(new OpenIdConnectAuthenticationOptions { // Other options removed for readability SaveTokens = true, // Required for the authorization code flow to exchange for tokens automatically RedeemCode = true });

  • 在控制器中读取访问令牌:
  • var result = await Request.GetOwinContext().Authentication.AuthenticateAsync("Cookies"); string token = result.Properties.Dictionary["access_token"];

    更多推荐

    如何使用owin和Mvc 5从httpcontext获取访问令牌

    本文发布于:2023-11-14 09:12:06,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1586808.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:令牌   如何使用   Mvc   owin   httpcontext

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!