I've run into a bit of a biff baff, using this new asp membership thingy on mvc5
I'm trying to determine what role a user is in and then, provided the role condition is met, show a button. I've got the code for the button hooked up as follows:
<p>
@if(User.IsInRole("canEdit"))
{
@Html.ActionLink("Create New", "Create")
}
</p>
Nice and simple, show the create button if user is in 'canEdit' role, right?... Wrong...
When i put this into my razor (chstml or whatever it is). file and load the page I get the following sql related error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Some things to note:
I have one dbcontext which has the following code:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
}
public class GameContext : IdentityDbContext<ApplicationUser>
{
public GameContext() : base("DefaultConnection", throwIfV1Schema: false)
{
}
public static GameContext Create()
{
return new GameContext();
}
The props have been removed for brevity.
-I have one connection string in the config file. -I can register, login logout, delete, use the isAuthenticated method ect.
I am truly baffled, any pointers on this would be great
Oddly enough I have just come across this very issue in our codebase. One of my developers was using the 'old' Role provider to check if user was in a particular role. This was causing the app to create a
LocalSql
database in App_Data as the config for all that still exists inmachine.config
. The connection string and provider details are specified inmachine.config
, so yourweb.config
will only add or remove them. So for example if yourmachine.config
says:And your
web.config
has this:Your application will be able to see and use both strings. To fix it you can clear the connection strings first like this:
But that still leaves the underlying problem that the old role (or profile) provider is being used in code. so you will instead get lots of errors when using it. You need to switch code like this:
To something like this (in my case
_userManager
is of typeUserManager<User>
and is injected into my controller constructor at runtime.