MVC5 simple membership/default membership

2019-06-04 22:15发布

问题:

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

回答1:

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 in machine.config. The connection string and provider details are specified in machine.config, so your web.config will only add or remove them. So for example if your machine.config says:

<connectionStrings>
    <add name="LocalSqlServer" connectionString="..."/>
</connectionStrings>

And your web.config has this:

<connectionStrings>
    <add name="MyConnectionString" connectionString="..."/>
</connectionStrings>

Your application will be able to see and use both strings. To fix it you can clear the connection strings first like this:

<connectionStrings>
    <clear />
    <add name="MyConnectionString" connectionString="..."/>
</connectionStrings>

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:

if (User.IsInRole("Admin"))
{
    //Do admin stuff
}

To something like this (in my case _userManager is of type UserManager<User> and is injected into my controller constructor at runtime.

var user = _userManager.FindById(User.Identity.GetUserId());

if (_userManager.IsInRole(user.Id, "Admin"))
{
        //Do admin stuff
}