我不能为ASP建立一个连接字符串。Net MVC v4.0采用标准的认证方法

时间:2022-09-22 16:07:34

Please I need help with this scenario:

我需要帮助这个场景:

I want to use the default ASP Membership/Authentication in my MVC app, connected to a VPN. In web.config I setup the connection "MyCustomConnection":

我想在我的MVC app中使用默认的ASP成员/身份验证,连接到一个VPN。在网络上。配置I设置连接“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>

Then in InitializeSimpleMembershipAttribute I have:

然后在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);
            }
        }
    }
}

When the debugger hit(After a click in the Login Hyperlink of the Basic Template) WebSecurity.InitializeDatabaseConnection("MyCustomConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true), threw an exception:

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

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

Additionally in User Context:

另外在用户上下文:

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

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

My question is:

我的问题是:

Why WebSecurity.InitializeDatabaseConnection("MyCustomConnection",... is trying to connect with the user of vpn and no with 'sa'(and his password 'sapass') as my connection string.

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

Thanks.

谢谢。

1 个解决方案

#1


1  

The issue is with the connection string, specifically the use of Trusted_Connection=True;Integrated Security=SSPI for more information, check here: Difference between Integrated Security = True and Integrated Security = SSPI

问题在于连接字符串,特别是Trusted_Connection=True的使用

#1


1  

The issue is with the connection string, specifically the use of Trusted_Connection=True;Integrated Security=SSPI for more information, check here: Difference between Integrated Security = True and Integrated Security = SSPI

问题在于连接字符串,特别是Trusted_Connection=True的使用