C# 与邮递员的api请求
我有一个mvc应用程序。我正在使用API进行检索。老师们 这是一节课:C# 与邮递员的api请求,c#,api,C#,Api,我有一个mvc应用程序。我正在使用API进行检索。老师们 这是一节课: [Authorize(Roles = IdentityRoles.Teacher)] [RoutePrefix("api//current")] public sealed class CurrentTeacherController : ApiControllerBase { private readonly ICurrentTeacherProcess _currentTeache
[Authorize(Roles = IdentityRoles.Teacher)]
[RoutePrefix("api//current")]
public sealed class CurrentTeacherController : ApiControllerBase
{
private readonly ICurrentTeacherProcess _currentTeacherProcess;
/// <summary>
/// Constructor.
/// </summary>
public CurrentTeacherController(ICurrentTeacherProcess process)
{
_currentTeacherProcess = process;
}
/// <summary>
/// Gets the teacher data of the current user
/// </summary>
/// <returns>The TeacherDto of the current teacher</returns>
[Route("")]
[HttpGet]
public TeacherDto GetTeacher()
{
return _currentTeacherProcess.GetTeacher();
}
}
这是startup.cs文件:
public sealed class Startup
{
/// <summary>
/// Configures the application for use with OWIN. This method is called implicitly by Microsoft.Owin.Host.SystemWeb.
/// </summary>
/// <param name="app"><see cref="IAppBuilder" />implementation.</param>
public void Configuration(IAppBuilder app)
{
var config = GlobalConfiguration.Configuration;
WebApiConfig.Register(config);
var container = AutofacConfig.Register(config);
app.UseAutofacLifetimeScopeInjector(container);
app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions
{
AuthenticationType = "Hallo",
AuthenticationMode = AuthenticationMode.Active,
TokenValidationParameters = ApiGatewaySecurityTokenHandler.CreateTokenValidationParameters(
"hoi", "hoi", IdentityRoles.Duo, IdentityRoles.Competent, IdentityRoles.Berichtenbox),
TokenHandler = container.Resolve<JwtSecurityTokenHandler>()
});
app.UseStageMarker(PipelineStage.PostAuthenticate);
app.UseMiddlewareFromContainer<ApiGatewayMiddleware>();
app.UseAutofacWebApi(config);
app.UseWebApi(config);
}
}
公共密封类启动
{
///
///将应用程序配置为与OWIN一起使用。此方法由Microsoft.OWIN.Host.SystemWeb隐式调用。
///
///实施。
公共无效配置(IAppBuilder应用程序)
{
var config=GlobalConfiguration.Configuration;
WebApiConfig.Register(配置);
var container=AutofacConfig.Register(配置);
应用程序UseAutoFacLifetimeScope注射器(容器);
app.useJWTBeareAuthentication(新的JWTBeareAuthenticationOptions
{
AuthenticationType=“你好”,
AuthenticationMode=AuthenticationMode.Active,
TokenValidationParameters=ApiGatewaySecurityTokenHandler.CreateTokeValidationParameters(
“hoi”“hoi”“IdentityRoles.Duo,IdentityRoles.胜任,IdentityRoles.Berichtenbox),
TokenHandler=container.Resolve()
});
应用程序UseStageMarker(PipelineStage.PostAuthentication);
app.useMidleWareFromContainer();
app.UseAutofacWebApi(配置);
app.UseWebApi(配置);
}
}
使用startup.cs文件更新您将获得404,因为您的API中不存在该请求的处理程序。您需要像这样将id添加到API路由
[Route("{id}")]
[HttpGet]
public TeacherDto GetTeacher(int id/* or other type if you aren't using int as your primary key*/ )
{
return _currentTeacherProcess.GetTeacher(id);
}
您的请求应该是:
http://localhost:6598/api/register/teachers/current/1001
我真的不明白你的管理员在做什么为什么你认为api/register/teachers/current会匹配
api/register/teachers/current/文凭/1001/
?谢谢。但这不是重点。因为如果我做了一个收件员,我会收到这样一条消息:{“消息”:“此请求的授权已被拒绝。”}。但是我已经登录了可能您没有在http请求中发送授权的cookie或令牌,您正在使用哪种类型的授权?此:[授权(Roles=IdentityRoles.Teacher)]好的,您正在使用身份cookie身份验证,您的应用程序是否在Postman中生成了身份验证cookie?通常,您在startup.cs文件上定义应用程序cookie名称,但不太确定默认名称是哪个,但请确保邮递员拥有该名称。这完全不是您问题的主题,因为您没有完全有效的授权
http://localhost:6598/api/register/teachers/current/1001