标签: bearer-token

什么是OAuth 2.0承载令牌?

根据RFC6750- OAuth 2.0授权框架:承载令牌使用,承载令牌是:

具有属性的安全令牌,即拥有该令牌的任何一方("持票人")可以以任何其他拥有该令牌的方式使用该令牌.

对我来说,这个定义含糊不清,我找不到任何规范.

  • 假设我正在实现授权提供程序,我可以为持有者令牌提供任何类型的字符串吗?
  • 它可以是随机字符串吗?
  • 它必须是某些属性的base64编码吗?
    它应该散列吗?
  • 服务提供商是否需要查询授权提供程序才能验证此令牌?

谢谢你的指针.

oauth bearer-token

138
推荐指数
5
解决办法
13万
查看次数

Web API身份验证基本与承载

我在我的Web API应用程序中创建了基于JWT的身份验证.我无法弄清楚它们之间的区别

  1. 基本令牌
  2. 持票人令牌

有人可以帮帮我吗?

basic-authentication jwt bearer-token

63
推荐指数
2
解决办法
3万
查看次数

使用WebApi中的OAuth Bearer Tokens Generation和Owin将更多信息返回给客户端

我创建了一个WebApi和一个Cordova应用程序.我正在使用HTTP请求在Cordova应用程序和WebAPI之间进行通信.在WebAPI中,我实现了OAuth Bearer Token Generation.

public void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider(new UserService(new Repository<User>(new RabbitApiObjectContext()), new EncryptionService()))
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

    }
Run Code Online (Sandbox Code Playgroud)

这是在SimpleAuthorizationServerProvider实现中

 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
       context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });

        // A little hack. context.UserName contains the email
        var user = await _userService.GetUserByEmailAndPassword(context.UserName, context.Password);

        if (user == null)
        {
            context.SetError("invalid_grant", "Wrong email or …
Run Code Online (Sandbox Code Playgroud)

c# authentication owin asp.net-web-api2 bearer-token

62
推荐指数
2
解决办法
5万
查看次数

ASP.NET Core 2.0 JWT验证因"用户授权失败:(null)"错误而失败

我正在使用ASP.NET Core 2.0应用程序(Web API)作为JWT发行者来生成移动应用程序的令牌消费品.不幸的是,这个令牌无法由一个控制器验证,而另一个控制器可以验证(在同一个asp.net核心2.0应用程序中使用相同的验证设置).

所以我有一个有效且可以解码的令牌,具有所有必需的声明和时间戳.但是一个端点接受它,而另一个端点给我401错误和调试输出:

Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:信息:用户授权失败:(null).

[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed for user: (null).
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed for user: (null).
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
      Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes ().
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
      Executing ChallengeResult with authentication schemes ().
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[12]
      AuthenticationScheme: Bearer was challenged.
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Information: AuthenticationScheme: Bearer was challenged.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action MyController.Get (WebApi) in 72.105ms
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action MyController.Get (WebApi) in 72.105ms
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: …
Run Code Online (Sandbox Code Playgroud)

c# bearer-token asp.net-core asp.net-core-webapi asp.net-core-identity

41
推荐指数
5
解决办法
2万
查看次数

没有身份的ASP.NET Core 2.0 Bearer Auth

我想我在一天前开始在.NET核心2.0上实现一个自包含的持有者auth webapi时,有一个非常简单的目标,但我还没有得到任何远程工作.这是我要做的事情列表:

  • 实施受保护的令牌受保护的webapi
  • 从同一项目中的端点发出令牌和刷新令牌
  • 使用[Authorize]属性控制对api表面的访问
  • 不使用ASP.Net Identity(我有更轻量级的用户/会员需求)

我完全可以在登录时构建身份/声明/主体并将其添加到请求上下文中,但我没有看到如何在没有Identity的Core 2.0 webapi中发布和使用auth/refresh令牌的单个示例.我已经看到了没有Identity的1.x MSDN MSDN示例,但这并没有让我在理解上达到上述要求.

我觉得这可能是一个常见的场景,它应该不是很难(也许它不是,也许只是缺少文档/示例?).据我所知,IdentityServer4与Core 2.0 Auth不兼容,opendiddict似乎需要Identity.我也不想在单独的进程中托管令牌端点,而是在同一个webapi实例中.

任何人都可以向我指出一个具体的例子,或至少给出一些关于最佳步骤/选项的指导吗?

asp.net bearer-token asp.net-core

26
推荐指数
2
解决办法
1万
查看次数

结合使用MVC页面和Web API页面的身份验证?

我有一个MVC 5 Web应用程序,可以使用Login.cshtml页面登录并获取cookie并且登录正常.但是,我想使用Web API进行登录,然后(可能)设置一个cookie,以便我登录我的MVC页面...(或使用MVC登录登录然后访问Web API)但是web api返回一个bearer token而不是cookie令牌...所以这不起作用.有没有办法结合使用我的MVC页面和我的Web API页面的身份验证?

更新:

这不是一个真正的代码问题,而是一个概念问题.

普通的MVC网页会检查一个名为".AspNet.ApplicationCookie"的cookie,以确定请求者的身份.通过调用ApplicationSignInManager.PasswordSignInAsync生成此cookie.

另一方面,WebAPI调用检查名为Authorization的项目的请求标头...并使用该值来确定请求者标识.这是从WebAPI调用"/ Token"返回的.

这些是非常不同的价值观.我的网站需要同时使用MVC页面 WebAPI调用(以动态更新这些页面)......并且都需要进行身份验证才能执行其任务.

我能想到的唯一方法是实际验证两次......一次是使用WebAPI调用,另一次是使用Login帖子.(见下面的答案).

这似乎非常hacky ...但我不明白授权代码足以知道是否有更合适的方法来实现这一点.

cookies asp.net-mvc asp.net-web-api bearer-token

23
推荐指数
1
解决办法
1万
查看次数

如何正确使用Bearer令牌?

我正在制作一个授权系统PHP,我遇到了传递JWT令牌的Bearer方案,我读了RFC 6750.我有以下疑惑:

  1. 这是如何改善安全性的?
  2. 在成功授权和登录后,服务器在其主体中响应客户端使用JWT令牌,现在当客户端发出另一个请求时,我不清楚如何实际执行此操作,我想在客户端的Authorization头中发送令牌请求,所以我现在应该只将"Bearer"加到我在服务器上一次响应中收到的令牌前面,如果是,那么服务器在收到Authorization标头时,应该只用空格分割字符串,然后取出第二个值获得的数组然后解码吗?例如Authorization: Bearer fdbghfbfgbjhg_something,服务器应该如何处理这个问题decodeFunc(explode(" ", $this->getRequest()->getHeader("Authorization"))[1])

php authentication oauth http-headers bearer-token

23
推荐指数
1
解决办法
3万
查看次数

从Web API的持有者令牌返回用户角色

我正在开发一个Web API 2项目.对于身份验证,我使用的是承载令牌.在成功验证后,API返回JSON对象.

{"access_token":"Vn2kwVz...",
   "token_type":"bearer",
   "expires_in":1209599,
   "userName":"username",
   ".issued":"Sat, 07 Jun 2014 10:43:05 GMT",
   ".expires":"Sat, 21 Jun 2014 10:43:05 GMT"}
Run Code Online (Sandbox Code Playgroud)

现在我想在这个JSON对象中返回用户角色.为了从JSON响应中获取用户角色,我需要做出哪些更改?

asp.net authentication asp.net-web-api2 bearer-token

22
推荐指数
2
解决办法
2万
查看次数

如何在客户端获取DotNetOpenAuth.OAuth2返回的错误消息?

我正在使用ExchangeUserCredentialForToken函数从授权服务器获取令牌.当我的用户存在于我的数据库中时,它工作正常,但是当凭据是incorect时,我想向客户端发回消息.我正在使用以下2行代码来设置错误消息:

context.SetError("Autorization Error", "The username or password is incorrect!");
context.Rejected();
Run Code Online (Sandbox Code Playgroud)

但在客户端,我只得到协议错误(错误400).您能帮我解决一下如何在授权服务器上获取服务器端设置的错误消息?

来自授权服务器的完整应用程序配置:

using Constants;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.Infrastructure;
using Microsoft.Owin.Security.OAuth;
using Owin;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Security.Claims;
using System.Security.Principal;
using System.Threading.Tasks;
using AuthorizationServer.Entities;
using AuthorizationServer.Entities.Infrastructure.Abstract;
using AuthorizationServer.Entities.Infrastructure.Concrete;

namespace AuthorizationServer
{
    public partial class Startup
    {
        private IEmployeeRepository Repository;  
        public void ConfigureAuth(IAppBuilder app)
        {
            //instanciate the repository
            Repository = new EmployeeRepository();

            // Enable Application Sign In Cookie
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = …
Run Code Online (Sandbox Code Playgroud)

c# dotnetopenauth asp.net-web-api bearer-token

21
推荐指数
2
解决办法
1万
查看次数

OAuthAuthorizationServerProvider实现中的Autofac依赖注入

我正在创建一个Web Api应用程序,我想使用承载令牌进行用户身份验证.我实现了令牌逻辑,按照这篇文章,一切似乎都运行正常.注意:我没有使用ASP.NET身份提供程序.相反,我为它创建了一个自定义用户实体和服务.

 public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);

        var config = new HttpConfiguration();
        var container = DependancyConfig.Register();
        var dependencyResolver = new AutofacWebApiDependencyResolver(container);
        config.DependencyResolver = dependencyResolver;

        app.UseAutofacMiddleware(container);
        app.UseAutofacWebApi(config);

        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
    }

    public void ConfigureOAuth(IAppBuilder app)
    {
        var oAuthServerOptions = new OAuthAuthorizationServerOptions
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(oAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());

    }
}
Run Code Online (Sandbox Code Playgroud)

这是我对SimpleAuthorizationServerProvider类的实现

private IUserService _userService;
    public IUserService UserService
    {
        get …
Run Code Online (Sandbox Code Playgroud)

dependency-injection autofac owin asp.net-web-api2 bearer-token

20
推荐指数
2
解决办法
5724
查看次数