Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I came across a Article on Web for Asp.net Identity here.

They have this code:


C#
public class AppUserManager : UserManager<AppUser> {

    public AppUserManager(IUserStore<AppUser> store)
     : base(store) {
     }

     public static AppUserManager Create(
     IdentityFactoryOptions<AppUserManager> options,
     IOwinContext context) {
         AppIdentityDbContext db = context.Get<AppIdentityDbContext>();
         AppUserManager manager = new AppUserManager(new UserStore<AppUser>(db));
         return manager;
     }
 } 


1.) The AppUser class is custom class deriving from the IdentityUser Class.
2.) The AppIdentityDbContext is deriving from the IdentityDbContext class.



Here we are creating a custom AppManagerClass which is derieved from UserManager class of Asp.net Identity this class has a method to create an instance of the AppUserManager class.

Now i want to know what these two lines are doing.

C#
AppIdentityDbContext db = context.Get&lt;AppIdentityDbContext&gt;();
AppUserManager manager = new AppUserManager(new UserStore&lt;AppUser&gt;(db));

They are Invoking the base contructor by passing in the new UserStore class instance and that UserStore class require DbContext type to be passed on.

But i am unable to get what is required from this UserStore class.

Also please tell me what this line of code is returning.

C#
<pre>AppIdentityDbContext db = context.Get<AppIdentityDbContext>();
Posted
Updated 19-Aug-15 2:11am
v2

1 solution

You must ask such questions under the article or blog post itself, it helps the authors to maintain their article contents to help the readers and can also help any other reader if he has the same question. Asking a question on a separate forum doesn't help you, readers or anyone.

ASP.NET Identity is a full interface based authentication provider, there are many services, stores, managers in it which you can use for your own needs. Entity Framework then comes in handy to handle the data sources for your application. ASP.NET no longer only requires SQL Server for data source, you can use any data source. For that sake, Entity Framework is used to manage the data sources.

The UserStore[^] is a component that is well described by MSDN as,
Quote:
Represents an Entity Framework implementation of a user store that supports IUserStore, IUserLoginStore, IUserClaimStore and IUserRoleStore.

You would need to learn the Identity framework, there are many things that cannot be explained in a compact answer. You should start learning from MSDN, here[^].

Edit

As the name suggests (and what I said in the previous content) the UserStore is actually the data source from where your user details and records would be fetched. You can define your own data sources. The class actually is present in Entity Framework. In your own application you implement the IUserStore interface and add additional features for your own application.

For more in-depth discussion and overview of this object in ASP.NET, please read the ASP.NET guide at http://www.asp.net/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity#userstore[^].
 
Share this answer
 
v2
Comments
lokopi.nagar 19-Aug-15 8:31am    
i do not know but why you have not answered the question, answering the question would have helped all those who searched on google, i am bit unclear from msdn article about this class that is why i asked here.
Afzaal Ahmad Zeeshan 19-Aug-15 8:33am    
Did you try to read the answer? I have provided the solution, with the link to UserStore object itself. Do you want me to show you a code sample here now? :-)

Readers of that article must be provided with a solution there. And anyone Googling will be able to find that MSDN article, easily! ;)
lokopi.nagar 19-Aug-15 8:54am    
I think a little more description would be helpful :)
Afzaal Ahmad Zeeshan 19-Aug-15 10:48am    
Please see the edit section. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900