【问题标题】:If Authorize fail in Web-api (asp.net) , browser show basic authentication如果在 Web-api (asp.net) 中授权失败,浏览器显示基本身份验证
【发布时间】:2015-02-11 06:16:52
【问题描述】:

我只想得到错误消息。 但应用程序显示它需要基本登录。

如何通过?

AccountServiceController

[Authorize]
[Route("UserRoles")]
public async Task<IHttpActionResult> GetUserRoles(string userName){
        //my code
}

Strat.Auth

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            ExpireTimeSpan = System.TimeSpan.FromDays(1)
        }); 
        var OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/Token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromHours(1),
            Provider = new DrDB.Identity.Infrastructure.SimpleAuthorizationServerProvider()
        };

        app.UseOAuthAuthorizationServer(OAuthServerOptions);

        OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
        app.UseOAuthBearerAuthentication(OAuthBearerOptions);

【问题讨论】:

  • 需要更多细节,更多关于如何设置身份验证的代码。
  • @Amit 我更新了我的代码

标签: asp.net authentication asp.net-web-api identity


【解决方案1】:

你做错了。

WebAPI 不应该显示登录提示 - 调用应用程序应该。

如果您检查 WebAPI 返回的响应,则可能是 401(未经授权)。

调用者可以检查 401 响应,并显示它需要的任何登录页面。

【讨论】:

  • 我不需要登录页面,但是当 web-api 返回 401 时会出现登录提示。而当我在 Visual Studio 上测试时,不会出现。但是当我在测试服务器上发布时它会出现。
猜你喜欢
  • 2012-10-02
  • 2012-09-10
  • 1970-01-01
  • 2012-05-27
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
相关资源
最近更新 更多