I am writing an application on ASP.NET MVC.
It took a list of all registered users. To display the list, I decided to use Membership.GetAllUsers()
, but I get an exception:
There was a network or instance error while connecting to SQL Server. Server not found or unavailable. Ensure that the instance name is correct and that remote connections are allowed on SQL Server. (provider: SQL Network Interfaces, error: 26 - Error finding the specified server or instance)
Tell me what code is needed? Database localdb
, trying to get a list of users through Membership.GetAllUsers()
in the controller and pass to the form. There are tables in the database, the connection is normal if you connect via the DatabaseContext db = new DatabaseContext()
object DatabaseContext db = new DatabaseContext()
. But in this way I can not get the role of users.
<connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-SoftwareDevelopmentCustom;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-SoftwareDevelopmentCustom.mdf" providerName="System.Data.SqlClient" /> </connectionStrings>
Code:
using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; using System.Threading; using System.Web.Mvc; using WebMatrix.WebData; using SoftwareDevelopmentCustom.Models; using System.Web.Security; namespace SoftwareDevelopmentCustom.Filters { [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false, Inherited = true)] 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) { // ΠΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΈΡ ASP.NET Simple Membership ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡ ΠΎΠ΄ΠΈΠ½ ΡΠ°Π· ΠΏΡΠΈ ΡΡΠ°ΡΡΠ΅ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock); } private class SimpleMembershipInitializer // ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ ΠΊΠ»Π°ΡΡΠ° ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·Π°ΡΠΎΡΠ° { public SimpleMembershipInitializer() { Database.SetInitializer<DatabaseContext>(null); try { using (var context = new DatabaseContext()) { if (!context.Database.Exists()) { // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π±Π°Π·Ρ Π΄Π°Π½Π½ΡΡ
SimpleMembership Π±Π΅Π· ΠΏΡΠΈΠΌΠ΅Π½Π΅Π½ΠΈΡ ΠΌΠΈΠ³ΡΠ°ΡΠΈΠΈ Entity Framework ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); } } // ΠΠ°ΡΡΡΠΎΠΉΠΊΠ° ASP.NET Simple Membership // 1 ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ - ΠΈΠΌΡ ΡΡΡΠΎΠΊΠΈ ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΡ ΠΊ Π±Π°Π·Π΅ Π΄Π°Π½Π½ΡΡ
. // 2 ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ - ΡΠ°Π±Π»ΠΈΡΠ°, ΠΊΠΎΡΠΎΡΠ°Ρ ΡΠΎΠ΄Π΅ΡΠΆΠΈΡ ΠΈΠ½ΡΠΎΡΠΌΠ°ΡΠΈΡ ΠΎ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡ
// 3 ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ - ΠΈΠΌΡ ΠΊΠΎΠ»ΠΎΠ½ΠΊΠΈ Π² ΡΠ°Π±Π»ΠΈΡΠ΅, ΠΊΠΎΡΠΎΡΠ°Ρ ΠΎΡΠ²Π΅ΡΠ°Π΅Ρ Π·Π° Ρ
ΡΠ°Π½Π΅Π½ΠΈΠ΅ Π»ΠΎΠ³ΠΈΠ½Π° // 4 ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡ - autoCreateTables Π°Π²ΡΠΎΠΌΠ°ΡΠΈΡΠ΅ΡΠΊΠΎΠ΅ ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΡΠ°Π±Π»ΠΈΡ Π΅ΡΠ»ΠΈ ΠΎΠ½ΠΈ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΡΡ Π² Π±Π°Π·Π΅ WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); SimpleRoleProvider roles = (SimpleRoleProvider)Roles.Provider; SimpleMembershipProvider membership = (SimpleMembershipProvider)Membership.Provider; // ΠΡΠΎΠ²Π΅ΡΠΊΠ° Π½Π°Π»ΠΈΡΠΈΡ ΡΠΎΠ»ΠΈ Admin if (!roles.RoleExists("Admin")) { roles.CreateRole("Admin"); } // ΠΠΎΠΈΡΠΊ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ Ρ Π»ΠΎΠ³ΠΈΠ½ΠΎΠΌ admin if (membership.GetUser("admin", false) == null) { membership.CreateUserAndAccount("admin", "qwe123"); // ΡΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ roles.AddUsersToRoles(new[] { "admin" }, new[] { "Admin" }); // ΡΡΡΠ°Π½ΠΎΠ²ΠΊΠ° ΡΠΎΠ»ΠΈ Π΄Π»Ρ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ } } 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); } } } } }