MVC5简单的会员/会员默认 [英] MVC5 simple membership/default membership

查看:149
本文介绍了MVC5简单的会员/会员默认的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰到一个有点一击BAFF,用在mvc5这个新的ASP会员啄

I've run into a bit of a biff baff, using this new asp membership thingy on mvc5

我试图确定用户是再什么样的角色,只要满足条件的作用,显示一个按钮。
我已经得到了code为迷上了按钮如下:

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>

尼斯和简单,显示创建按钮,如果用户在canEdit的角色,对不对?......不对......

Nice and simple, show the create button if user is in 'canEdit' role, right?... Wrong...

当我把这个在我的剃须刀(chstml或不管它是什么)。文件并加载网页,我得到下面的SQL相关的错误:

When i put this into my razor (chstml or whatever it is). file and load the page I get the following sql related error:

发生-特定实例的网络相关的或错误而
  与SQL Server建立连接。服务器没有被发现或
  无法访问。验证实例名称是否正确,以及
  SQL Server配置为允许远程连接。 (提供者:SQL
  网络接口,错误:26 - 错误定位服务器/实例
  指定)

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)

需要注意以下几点:

我有具有以下$ C $一个系统一台的DbContext:

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可以注册,登录注销,删除,使用isAuthenticated方法等。

-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

推荐答案

奇怪的是我刚刚碰到过这个问题非常在我们的codeBase的。我的一个开发者用的是'老'角色提供程序来检查用户是在一个特定的角色。这是导致应用程序的App_Data中创建一个 LocalSql 数据库的配置为所有在的machine.config 仍然存在。连接字符串和提供细节的machine.config 指定的,所以你的的web.config 只会增加或移除。因此,例如,如果你的的machine.config 说:

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>

和您的的web.config 有这样:

<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>

但是,这仍留下了旧角色(或个人)提供在code使用的根本问题。因此使用它的时候,你反而会得到很多的错误。你需要切换code是这样的:

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
}

要像这样(在我的情况 _userManager 的类型为的UserManager&lt;使用者&GT; ,并注入到我的在运行时的控制器构造。

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
}

这篇关于MVC5简单的会员/会员默认的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆