开发者

AuthorizeRole="Admin" not working in mvc3 app?

  • 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开发者_如何学Python:

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?


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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜