发布时自定义授权属性的问题

Problems with custom authorize attribute on published

我正在使用带有 ssl 的基本身份验证,当我调试我的网络时 api 一切似乎都工作正常。 IsAuthorized 方法被调用,目前它正在运行。但是当我将 webapi 发布到服务器时,我收到以下错误消息

An error has occurred. No connection to Sql database. System.Web.HttpException System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString)

我什至不知道它为什么要尝试创建 mdf 文件,因为 webapi 使用的是 sql server express 实例。如果我删除授权属性,一切正常。

protected override bool IsAuthorized(HttpActionContext actionContext)
    {
        if (Thread.CurrentPrincipal.Identity.Name == null || Thread.CurrentPrincipal.Identity.Name.Length == 0)
        { // If an identity has not already been established by other means:
            AuthenticationHeaderValue auth = actionContext.Request.Headers.Authorization;
            if (auth != null && string.Compare(auth.Scheme, "Basic", StringComparison.OrdinalIgnoreCase) == 0)
            {
                string credentials = UTF8Encoding.UTF8.GetString(Convert.FromBase64String(auth.Parameter));
                int separatorIndex = credentials.IndexOf(':');
                if (separatorIndex >= 0)
                {
                    string email = credentials.Substring(0, separatorIndex);
                    string password = credentials.Substring(separatorIndex + 1);

                    //using Facade to get access to database and check credentials 
                    if (//check if valid)
                    {            
                        Thread.CurrentPrincipal = actionContext.ControllerContext.RequestContext.Principal = new GenericPrincipal(new GenericIdentity(email, "Basic"), System.Web.Security.Roles.Provider.GetRolesForUser(email));
                    }
                }
            }
            return base.IsAuthorized(actionContext);
        }

如果有人能帮助我,我将非常高兴!

所以我根据以下教程通过重新实现自定义授权解决了这个问题

https://weblog.west-wind.com/posts/2013/Apr/18/A-WebAPI-Basic-Authentication-Authorization-Filter