AuthorizeRole="管理员"无法在 mvc3 应用程序中工作?

发布于 2024-12-08 22:17:43 字数 933 浏览 0 评论 0 原文

  • 我在 mvc3 应用程序中设置了会员资格提供程序。
  • 我将它从本地sql express app_data/aspnetdb.mdf移植到本地服务器2008实例 (目前这对于登录/等工作正常,并且 [Authorize] SomeMethod()
  • 我最近在 aspnet_UsersInRoles 关联中添加了 1 个新的 aspnet_roles (“管理员”)和 1 个新条目。w/我的用户名。

角色表:

ApplicationId RoleId RoleName LoweredRoleName 描述 3F96CA96-CCB3-4780-8038-AF3CCE0BD4F2 9B5B798D-E56E-4144-A12C-7C8945FCB413 管理员管理员管理员

UsersInRoles:

用户 ID 角色 ID 58974159-E60E-4185-AD00-F8024C7C5974 9B5B798D-E56E-4144-A12C-7C8945FCB413

问题1:为什么以下代码不允许我进入此控制器操作?

    [Authorize]
    [Authorize(Roles = "Admin")]  // !!! This is keeping me out...commented out I can get in but I'm set as "Admin" and have tried lower case "admin" as well.
    public ActionResult SomeMethod()
    {}

Q1a:是否需要在 Web 配置中设置另一个角色提供程序连接字符串才能使其正常工作?

  • I have membership provider setup in an mvc3 application.
  • I ported it from the local sql express app_data/aspnetdb.mdf to a local server 2008 instance
    (this is currently working fine for logins/etc, and [Authorize] SomeMethod()
  • I recently added 1 new aspnet_roles ("Admin") and 1 new entry in the aspnet_UsersInRoles assoc. w/my username.

Role Table:

ApplicationId RoleId RoleName LoweredRoleName Description
3F96CA96-CCB3-4780-8038-AF3CCE0BD4F2 9B5B798D-E56E-4144-A12C-7C8945FCB413 Admin admin Administrator

UsersInRoles:

UserId RoleId
58974159-E60E-4185-AD00-F8024C7C5974 9B5B798D-E56E-4144-A12C-7C8945FCB413

Q1: Why does the following code not let me into this controller action?

    [Authorize]
    [Authorize(Roles = "Admin")]  // !!! This is keeping me out...commented out I can get in but I'm set as "Admin" and have tried lower case "admin" as well.
    public ActionResult SomeMethod()
    {}

Q1a: Is there another a role provider connection string that I have to setup in the web config to get this to work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一念一轮回 2024-12-15 22:17:43

您能否发布您的 web.config 中有关成员资格和角色提供者的部分内容。这听起来像是您的应用程序名称设置不正确。

 <roleManager enabled="true">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="Your Application Name here" />
      </providers>
    </roleManager>

编辑
您的应用程序名称未设置。它需要设置为某些内容,并且必须与数据库表中的应用程序名称匹配:aspnet_Applications

如果您的 Web 配置有 applicationName="TestApp" />那么您的数据库表还需要在 aspnet_Applications 表中有 TestApp

Can you please post parts of your web.config regarding membership and role providers. This sounds like your application name isn't set properly.

 <roleManager enabled="true">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="Your Application Name here" />
      </providers>
    </roleManager>

Edit
Your application name isn't set. It needs to be set to something and it must match the application name in your database table: aspnet_Applications

If you web config has applicationName="TestApp" /> then your database table also needs to have TestApp in the aspnet_Applications table

瀞厅☆埖开 2024-12-15 22:17:43

确保您的 ApplicationId 与您可能使用不同的 applicationId 相同,请检查此处 了解更多信息

Make sure that your ApplicationId is the same you are probably using a different applicationId, check here for more info

岁月如刀 2024-12-15 22:17:43

请尝试使用以下代码来查明您的问题:

public ActionResult SomeMethod() {

    if (!User.IsInRole("Admin")) 
        throw new SecurityException("User is not an admin.");

}

如果出现异常,则意味着您的会员提供商存在问题,或者您不处于 admin 角色,或者您的 applicationid 配置为不同下面建议。

这些是一些可能性。

Try the below code in order to pin point your problem :

public ActionResult SomeMethod() {

    if (!User.IsInRole("Admin")) 
        throw new SecurityException("User is not an admin.");

}

If you get an exception, it means that there is problem with your Membership Provider or you're not in admin role or your applicationid was configured different as suggested below.

These are some of the possibilities.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文