2

I have setup an MVC website with Forms Authentication.

Basically, here is the code that has error which says that there is no such user '2010108703' but still, it is logged on (we can see there is a value at User.Identity.Name)

 string user = User.Identity.Name;
            Roles.AddUserToRole(user, "student");
            return View();

Here is my web.config

 <roleManager enabled ="true" defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
</roleManager>

so as you can see, we have a value for the current user which is "2010108703"

enter image description here

but when we are trying to add the role, it says, no such user exist.

enter image description here

Is it a bug? why is that so?

Thanks

2
  • did you try clearing the cookie of your browser? Commented Jun 25, 2014 at 4:24
  • yes softsan i did tried nothing changed Commented Jun 25, 2014 at 4:30

2 Answers 2

1

FormsAuthentication is a separate system from Membership/Roles. You can put anything you want in the cookie and have the user be logged in, that doesn't mean they exist in your database.

You need to check your login code to make sure that it's actually validating the user against the membership database, and that both membership and roles are using the same connection string.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm just taking a wild guess.

Since your creating a new ASP.Net MVC, most likely it comes with ASP.Net Identity instead of legacy ASP.Net Membership Provider. FYI: Look at the database tables.

If so, User.Identity IPrincipal object is still available in ASP.Net Identity.

However, Roles object is not available anymore. Instead, you need to call UserManager object.

For example,

var roleResult = await UserManager.AddUserToRolesAsync(user, new []{"student"});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.