簡體   English   中英

僅在生產環境中“對此請求的授權被拒絕”

[英]“Authorization has been denied for this request” in production environment only

我正在通過以下身份驗證設置使用WebAPI和Owin。 在本地,我可以注冊,獲取令牌並發出帶有[Authorize]批注的API請求。 在生產環境中,我可以注冊並獲得令牌 ,但是當我嘗試使用[Authorize]批注打到端點時,標題中會出現錯誤。

鑒於它在本地工作,我懷疑這是一個代碼問題。 並且我將所有.dll生成並部署到生產服務器,因此引用都應為同一版本。 IIS配置會有所不同嗎? 將兩者進行比較,我看不到有什么東西對我跳來跳去,但我可能會丟失一些東西。

並且在需要解決的情況下,我使用Visual Studio中的Web Deploy將站點推送到生產環境。 除了為生產環境轉換連接字符串外,我還對兩種環境使用完全相同的web.config。

啟動文件

[assembly: OwinStartup(typeof(MyProject.API.Startup))]
namespace MyProject.API
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

App_Start \\ Startup.Auth.cs

namespace MyProject.API
{
    public partial class Startup
    {
        public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }

        public static string PublicClientId { get; private set; }

        public void ConfigureAuth(IAppBuilder app)
        {
            // Allow access_token to be passed as a query parameter
            app.Use(async (context, next) =>
            {
                if (context.Request.QueryString.HasValue)
                {
                    if (string.IsNullOrWhiteSpace(context.Request.Headers.Get("Authorization")))
                    {
                        var queryString = HttpUtility.ParseQueryString(context.Request.QueryString.Value);
                        string token = queryString.Get("access_token");

                        if (!string.IsNullOrWhiteSpace(token))
                        {
                            context.Request.Headers.Add("Authorization", new[] { string.Format("Bearer {0}", token) });
                        }
                    }
                }

                await next.Invoke();
            });

            app.CreatePerOwinContext(MyProjectDbContext.Create);
            app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            PublicClientId = "self";
            OAuthOptions = new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath = new PathString("/Token"),
                Provider = new ApplicationOAuthProvider(PublicClientId),
                AuthorizeEndpointPath = new PathString("/account/ExternalLogin"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),
                AllowInsecureHttp = true
            };
            app.UseOAuthBearerTokens(OAuthOptions);
        }
    }
}

Global.asax.cs

protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
    DependencyInjectorConfig.RegisterDependencies();
    MappingConfig.RegisterMappings(); // Autofac stuff
    Database.SetInitializer(new MyProjectInitializer());
}

啊! 登錄調用使用本地api,而其余路由使用生產api。 修復解決了我的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM