Coder Social home page Coder Social logo

Comments (3)

MisinformedDNA avatar MisinformedDNA commented on June 2, 2024 1

Right, it says

Your user and role entities must inherit from MongoIdentityUser and MongoIdentityRole

but it doesn't call out that MongoIdentityUser doesn't inherit from IdentityUser, nor does it call out that the partial needs to be updated. These should be called out in the README.

That's why I converted the MVC sample that was in the MSFT repo

Samples are nice, but is it your intention that we copy your sample as a starting point? Maybe that works fine for MVC, but that leaves out people who are developing Razor Pages or Blazor apps. Because of that, the README should call out the bare minimum to get started, regardless of project type.

from aspnetcore.identity.mongodbcore.

MisinformedDNA avatar MisinformedDNA commented on June 2, 2024

I figured it out, though I don't like the solution...

First IdentityUser is used in all the default Identity templates, so, by default, _LoginPartial.cshtml is looking for a binding to UserManager<IdentityUser> and no matching service could be found.

Looking at the inheritance chain, I found that

  • MongoIdentityUser inherits from IdentityUser<Guid>
  • IdentityUser inherits from IdentityUser<string>

So that explains why this error keeps showing up: MongoIdentityUser doesn't actually inherit from IdentityUser.

I don't know how you can clearly communicate this through code, so at the very least, the documentation should show how to convert a default Identity template to use this provider instead.

from aspnetcore.identity.mongodbcore.

alexandre-spieser avatar alexandre-spieser commented on June 2, 2024

Hello thanks for your message. Not sure what the issue is here.
the documentation should show how to convert a default Identity template to use this provider instead. <= That's why I converted the MVC sample that was in the MSFT repo :
https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore/tree/b3db4eb39fcd2d1f0df59ffbb6933dc53f794476/sample/MongoIdentitySample.Mvc.

Where the partial is indeed different:
https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore/blob/master/sample/MongoIdentitySample.Mvc/Views/Shared/_LoginPartial.cshtml

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

UserManager<IdentityUser> cannot work in this package.
As mentioned in the docs:

Your user and role entities must inherit from MongoIdentityUser<TKey> and MongoIdentityRole<TKey> in a way similar to the IdentityUser<TKey> and the IdentityRole<TKey> in Microsoft.AspNetCore.Identity, where TKey is the type of the primary key of your document.

If you really want string primary keys:

public class ApplicationUser: MongoIdentityUser<string>
{
	public MyUser () : base()
	{
	}

	public MyUser (string userName, string email) : base(userName, email)
	{
	}
}

with matching role:

public class ApplicationRole : MongoIdentityRole<string>
{
	public ApplicationRole() : base()
	{
	}

	public ApplicationRole(string roleName) : base(roleName)
	{
	}
}

then:

services.AddIdentity<ApplicationUser, ApplicationRole>()
	.AddMongoDbStores<ApplicationUser, ApplicationRole, string>
	(
		"mongodb://localhost:27017",
		"MongoDbTests"
	)
	.AddDefaultTokenProviders();

As highlighted in:
https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore#user-and-role-entities

If you feel like the docs are not clear enough please feel free to raise a PR. I understand your frustration but unfortunately I cannot satisfy everyone. I advise you to copy the sample and use it as a boiler plate for your project to avoid issues.

I hope this makes sense.

from aspnetcore.identity.mongodbcore.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.