如果在Web-api(asp.net)中授权失败,浏览器将显示基本身份验证

时间:2015-02-11 06:16:53

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

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

我该如何通过?

Error

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);

1 个答案:

答案 0 :(得分:0)

你这是错误的做法。

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

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

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