我想使用 postman 登录我的应用程序,并在登录时收到一个 token ,以便使用该 token 进行其他 api 调用。我尝试过 {locahost}/connect/token 但收到 405 错误。我如何登录我的 使用 postman 的应用程序感谢您的帮助。
这是我生成应用程序的方式
- 我打开 Visual Studio 2019。
- 我选择了“创建新项目”
- 选择 ASP.NET Core 和 Angular
- 点击下一步
- 给它起个名字
- 将其保存在您想要的位置
- 在“身份验证类型”下拉列表中选择“个人帐户”
应用程序创建后,登录和注册就可以正常工作,但我想在 Postman 中调用这些 api。
这是 Visual Studio 为我生成的启动项。
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<erp_colombiaDbContext>(options =>
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<erp_colombiaDbContext>();
services.AddIdentityServer()
.AddApiAuthorization<ApplicationUser, erp_colombiaDbContext>();
services.AddAuthentication()
.AddIdentityServerJwt();
services.AddControllersWithViews();
services.AddRazorPages();
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/dist";
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
if (!env.IsDevelopment())
{
app.UseSpaStaticFiles();
}
app.UseRouting();
app.UseAuthentication();
app.UseIdentityServer();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
app.UseSpa(spa =>
{
// To learn more about options for serving an Angular SPA from ASP.NET Core,
// see https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
}
}
我看过这个答案Default settings for AddApiAuthorization (Scaffolded Angular + IdentityServer4)但我不明白他从哪里获得填写 postman 表格“获取新访问 token ”的值
最佳答案
发布此内容是为了回应上述评论。
在 this question ,来自用户的回答@Lasanga Guruge指出基本支架应用程序的默认值。所以这些设置应该适合您在 Postman 中
Grantype of Authorization Code
ClientId will be corresponding to the application name. That can be changed in appsettings.json (Clients) and api-authorization.constants.ts (ApplicationName)
By default a client secret would not be applied to the client
Also pkce is enabled.
No default credentials
Additional scope named {ProjectName}API will be added
我创建了一个基本的 ASP.Net Angular 项目并让它使用这些值
- 授权类型 - 授权代码(带有 PKCE)
- 回调 URL - https://localhost:{port}/authentication/login-callback
- 身份验证 URL - https://localhost:{port}/connect/authorize
- token URL - https://localhost:{port}/connect/token
- 客户端 ID - {ClientId}
- 范围 - {ClientId}API openid 配置文件
要获取您的客户端 ID,请查看 appsettings.json 中的 IdentityServer 部分 在下面的项目中,“Project2”是我的 clientID。它应该是您为项目命名的任何名称。
"IdentityServer": {
"Clients": {
"Project2": {
"Profile": "IdentityServerSPA"
}
}
}
更新
要使用 token 进行授权 API 调用,您需要确保在请求的 Authorization header 中设置它。
- Authorizaion =“持有者{ token }”
您应该能够通过将 Postmans 授权表单中的“将授权数据添加到”字段设置为“请求 header ”来进行设置。
关于c# - 使用 Postman 登录 Angular ASP.NET CORE 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68486433/