3

请在这种情况下需要帮助:

我想在连接到 VPN 的 MVC 应用程序中使用默认的 ASP 成员资格/身份验证。在 web.config 中,我设置了连接“MyCustomConnection”:

<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    ...
    <add name="MyCustomConnection" connectionString="Data Source=172.x.x.y\somewhere;Initial Catalog=The.Main.DB;User ID=sa;Password=sapass;Trusted_Connection=True;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
  </connectionStrings>

然后在InitializeSimpleMembershipAttribute我有:

public sealed class InitializeSimpleMembershipAttribute : ActionFilterAttribute
{
    private static SimpleMembershipInitializer _initializer;
    private static object _initializerLock = new object();
    private static bool _isInitialized;

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Ensure ASP.NET Simple Membership is initialized only once per app start
        LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
    }

    private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            Database.SetInitializer<UsersContext>(null);

            try
            {
                using (var context = new UsersContext())
                {
                    if (!context.Database.Exists())
                    {
                        // Create the SimpleMembership database without Entity Framework migration schema
                        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                    }
                }

                WebSecurity.InitializeDatabaseConnection("MyCustomConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
            }
        }
    }
}

当调试器点击(在基本模板的登录超链接中单击后)WebSecurity.InitializeDatabaseConnection("MyCustomConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true),抛出异常:

Cannot open database "The.Main.DB" requested by the login. The login failed.
Login failed for user 'VPN\MYUSER'.

另外在用户上下文中:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("MyCustomConnection")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

我的问题是:

为什么WebSecurity.InitializeDatabaseConnection("MyCustomConnection",...要尝试与 vpn 的用户连接,而不是使用“sa”(以及他的密码“sapass”)作为我的连接字符串。

谢谢。

4

1 回答 1

1

问题出在连接字符串上,特别是Trusted_Connection=True;Integrated Security=SSPI有关更多信息的使用,请在此处查看:Integrated Security = True 和 Integrated Security = SSPI 之间的区别

于 2013-02-01T16:52:57.540 回答