2017-01-07 9 views
0

WebApi 401の未承認の問題に関するいくつかの質問といくつかの回答があります。私はまだすべてがローカル環境でうまく動作する理由を理解できません。上記のエラーは生産時に発生します。WebApiプロダクションで無許可(401)

私はコードロジックのほとんどを投稿しているので、誰かが問題の原因とその解決方法を説明することができます。そして、正確で明確にしようとしてください。この回答を含むすべての回答:Owin Bearer Token Not Working for WebApi - はまだ私には不明です。

次のように、私は提供されたテンプレートをそのまま使用します。

だから、ここGlobal.asax.csでの通常のエントリ:以下

public class WebApiApplication : System.Web.HttpApplication 
{ 
    protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     GlobalConfiguration.Configure(WebApiConfig.Register); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 
    } 
} 

はStartup.csです:

public partial class Startup 
{ 
    public void Configuration(IAppBuilder app) 
    { 
     ConfigureAuth(app); 
    } 
} 

そして、ここではStartup.Auth.csファイルです

public partial class Startup 
{ 
    public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; } 

    public static string PublicClientId { get; private set; } 

    // For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864 
    public void ConfigureAuth(IAppBuilder app) 
    { 
     // Configure the db context and user manager to use a single instance per request 
     app.CreatePerOwinContext(ApplicationDbContext.Create); 
     app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 

     // Enable the application to use a cookie to store information for the signed in user 
     // and to use a cookie to temporarily store information about a user logging in with a third party login provider 
     app.UseCookieAuthentication(new CookieAuthenticationOptions()); 
     app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 

     // Configure the application for OAuth based flow 
     PublicClientId = "self"; 
     OAuthOptions = new OAuthAuthorizationServerOptions 
     { 
      TokenEndpointPath = new PathString("/Token"), 
      Provider = new ApplicationOAuthProvider(PublicClientId), 
      AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), 
      AccessTokenExpireTimeSpan = TimeSpan.FromDays(7), 
      // In production mode set AllowInsecureHttp = false 
      AllowInsecureHttp = bool.Parse(System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Oauth_AllowInsecureHttp")) 
     }; 

     // Enable the application to use bearer tokens to authenticate users 
     app.UseOAuthBearerTokens(OAuthOptions); 

     //****************** GOOGLE AUTHENTICATION ******************************************************* 
     app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions() 
     { 
      ClientId = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Oauth_Google:ClientID"), 
      ClientSecret = System.Web.Configuration.WebConfigurationManager.AppSettings.Get("Oauth_Google:ClientSecret") 

     }); 
    } 
} 

最後に、WebApiConfig:

public static class WebApiConfig 
{ 
    public static void Register(HttpConfiguration config) 
    { 
     // Elmah logging... 
     config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger()); 

     // Web API configuration and services 
     // Configure Web API to use only bearer token authentication. 
     config.SuppressDefaultHostAuthentication(); 
     config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType)); 

     // Web API routes 
     config.MapHttpAttributeRoutes(); 

     config.Routes.MapHttpRoute(
      name: "DefaultApi", 
      routeTemplate: "api/{controller}/{id}", 
      defaults: new { id = RouteParameter.Optional } 
     ); 
    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); 
    config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; 
    } 
} 

前述のとおり、すべてがローカルで正常に動作します。 401(承認された)問題は、リモートサーバーにデプロイされた場合にのみ発生します。私たちはPostmanとテストしましたが、まだ運がありません。応答ヘッダーには、次の情報が表示されます。 - サーバー→Microsoft-IIS/8.5; - だから、WWW認証→ベアラ、ネゴシエート、NTLM

は、間違いなく、 "認可:ベアラSomeTokenBlablablab" は動作するはずです....

はあなたの助けをいただき、ありがとうございます。

+1

こんにちは..あなたのwebconfigファイルも投稿できますか? ..そしてまた。あなたはそれがCORSの問題であると確信していませんか? –

答えて

0

最後に、CORSの設定(上記のコードに含まれていないコード)を確認した後で問題を解決しました。

関連する問題