【问题标题】:Token based authentication by Angular and Web API – 404 errorAngular 和 Web API 基于令牌的身份验证 – 404 错误
【发布时间】:2019-04-24 20:44:32
【问题描述】:

我正在使用 Angular 和 ASP.NET Web API。当我尝试基于令牌的身份验证并发送登录请求时,我收到以下错误:POST http://localhost:53339/token 404 (Not Found)

我尝试了多种解决方案。

webapi 启动类:

public void Configuration(IAppBuilder app)
{
    HttpConfiguration config = new HttpConfiguration();
    ConfigureOAuth(app);
    WebApiConfig.Register(config);
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    app.UseWebApi(config);
}

public void ConfigureOAuth(IAppBuilder app)
{
    // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); //For support cross region
    var testProvider = new Providers.MyAuthorizeProvider();
    OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
    {
        AllowInsecureHttp = true,
        TokenEndpointPath = new PathString("/token"),
        AccessTokenExpireTimeSpan = TimeSpan.FromDays(14.0),
        AuthorizationCodeExpireTimeSpan = TimeSpan.FromDays(14.0),
        RefreshTokenProvider = new Providers.RefreshTokenProvider(),
        Provider = testProvider ,
    };
    app.UseOAuthAuthorizationServer(options);
    app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}

我想要通过 Angular 和 Web API 进行基于令牌的身份验证。


当我尝试调用 api 时,它显示此 404 错误本地主机未找到

<rewrite> 
  <rules> 
    <rule name="RewriteRules" stopProcessing="true"> 
      <match url=".*"/>
      <conditions logicalGrouping="MatchAll">
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        <add input="{REQUEST_URI}" pattern="^/(api)" negate="true" /> 
      </conditions> 
      <action type="Rewrite" url="/index.html" /> 
    </rule> 
  </rules> 
</rewrite>

【问题讨论】:

  • 您是否验证过您的 ConfigureOAuth() 方法正在被调用?您是否使用任何类型的 URL 重写?
  • 当我尝试调用 api 时显示此 404 错误本地主机未找到
  • 明白,但这并不能回答我提出的问题。

标签: sql angular asp.net-web-api


【解决方案1】:

由于您使用的是 URL 重写规则,因此您需要为您的令牌端点 (/token) 添加一个例外。这是一个例子:

<rewrite>
    <rules>
        <rule name="RewriteRules" stopProcessing="true">
            <match url=".*" />
            <conditions logicalGrouping="MatchAll">
                <!-- EXISTING RULES -->
                <!-- Add this rule: -->
                <add input="{REQUEST_URI}" pattern="^/token" negate="true" />
            </conditions>
            <action type="Rewrite" url="/" />
        </rule>
    </rules>
</rewrite>

【讨论】:

  • 还是localhost:53339/token404 (未找到)
  • 当我尝试从邮递员那里检查 api throw error resource not found
  • 您刚刚粘贴了与已有的完全相同的重写规则。您是否为 /token 添加了附加规则?
猜你喜欢
  • 2017-04-19
  • 1970-01-01
  • 1970-01-01
  • 2015-11-18
  • 2016-07-31
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 2015-07-02
相关资源
最近更新 更多