Abp vNext 获取Token

本文介绍了一个获取Token的接口实现方式,包括接口地址、调用方式及所需参数等详细信息。重点讲解了如何使用Postman工具进行调用并获取Token。

获取Token:

1、接口地址:https://localhost:44399/connect/token

2、接口需要以:application/x-www-form-urlencoded 的方式调用;

3、接口参数:

//接口参数
{
    client_id: 'GasMonitoring_App',
    //client_secret: '1q2w3e*',//默认只有GasMonitoring_Web 需要clientsecret密码 看数据库配置
    grant_type: 'password',
    //scope: 'Mall',         //范围  申请token的时候最好不要 范围限制 否则后续调用接口会出现token未授权的情况
    username: 'admin',     //用户名
    password: 'Admin12345*'//密码
}

 

用postman获取token

 

 

 

ABP vNext 8.0中,配置IdentityServer4 OAuth2授权主要包括以下几个步骤: 1. **安装依赖**: 首先,确保你的项目已经安装了必要的NuGet包,如Microsoft.AspNetCore.IdentityServer and Microsoft.IdentityModel.Tokens。 ```sh dotnet add package Microsoft.AspNetCore.IdentityServer.EntityFrameworkCore dotnet add package Microsoft.AspNetCore.IdentityServer.AccessTokenValidation dotnet add package Microsoft.IdentityModel.Tokens ``` 2. **配置DbContext**: 创建一个IdentityServer所需的数据库上下文,并将其连接字符串添加到configuration中。例如,你可以创建一个`IdentityServerDbContext`,继承自`IdentityDbContext<ApplicationUser>`: ```csharp services.AddDbContext<IdentityServerDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); ``` 3. **实体模型**: 定义应用和服务提供者的实体模型,包括客户端(Client)、资源(Resource)和作用域(Scope)。这通常在`IdentityResources`和`ApiResources`中完成。 4. **配置服务**: 在`Startup.cs`中添加IdentityServer4服务并设置相关选项: ```csharp services.AddIdentityServer() .AddInMemoryClients(clientConfigurations) .AddInMemoryScopes(scopeConfigurations) .AddAspNetIdentity<ApplicationUser>() .AddDeveloperSigningCredential(); private static IEnumerable<Client> clientConfigurations => new[] { // ... 这里添加客户端配置,包括redirect_uri, client_id等 }; private static IEnumerable<ApiResource> scopeConfigurations => new[] { // ... 这里添加资源和作用域配置 }; ``` 5. **添加认证中间件**: 将IdentityServer4的认证中间件添加到HTTP请求管道中,以便处理OAuth2授权流程。 ```csharp app.UseIdentityServer(); ``` 6. **验证中间件**: 如果你想验证Access Tokens,需要添加`AddJwtBearer`中间件来验证接收到的令牌。 ```csharp app.Use(async (context, next) => { var authResult = await context.AuthenticateAsync("Bearer"); if (authResult?.Principal != null) { context.SetPrincipal(authResult.Principal); context.AuthenticationProperties = authResult.Properties; await next(); } else { context.Response.StatusCode = StatusCodes.Status401Unauthorized; await context.Response.WriteAsync("Unauthorized"); } }); ``` 7. **授权装饰器**: 在AppService中,使用`Authorize`装饰器来控制哪些操作需要验证Token才能访问。 ```csharp [Authorize] public class YourAppService : YourAppServiceBase { // ... } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值