{"id":1134,"date":"2013-10-20T19:54:43","date_gmt":"2013-10-20T19:54:43","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2013\/10\/20\/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes\/"},"modified":"2022-08-09T03:24:17","modified_gmt":"2022-08-09T10:24:17","slug":"building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes\/","title":{"rendered":"Building a simple ToDo application with ASP.NET Identity and associating Users with ToDoes"},"content":{"rendered":"<p><font size=\"2\">Hello everyone. I was prompted to write this post from a comment I received on <\/font><a href=\"http:\/\/blogs.msdn.com\/b\/webdev\/archive\/2013\/10\/17\/announcing-release-of-asp-net-and-web-tools-for-visual-studio-2013.aspx\"><font size=\"2\">http:\/\/blogs.msdn.com\/b\/webdev\/archive\/2013\/10\/17\/announcing-release-of-asp-net-and-web-tools-for-visual-studio-2013.aspx<\/font><\/a><font size=\"2\">. I am pasting the comment verbatim from the post<\/font><\/p>\n<p> <font size=\"2\"><\/font>  <\/p>\n<p><font size=\"2\">\u201cI&#8217;m having a lot of difficulty integrating the IdentityDbContext with other DbContexts to create a comprehensive model and database for my applications. If the membership system always stood on its own, there wouldn&#8217;t be an issue but the User ID typically gets used throughout other models and tables (think of a blog, for example, where some authenticated users are authorized to create blog posts whereas others are only authorized to leave comments &#8211; all users, though, need to have their User ID stored with blogs and\/or comments). What are the recommended ways to combine these EF Code First models?\u201d<\/font><\/p>\n<p> <font size=\"2\"><\/font>  <\/p>\n<p><font size=\"2\">While this requirement may seem fairly straight forward, it does highlight lots of key components of using any membership system in an application. I thank \u201cRobert Gaut\u201d for posting this comment which motivated me to write this post.<\/font><\/p>\n<p> <font size=\"2\"><\/font>  <\/p>\n<p><font size=\"2\">So this should set the context of this blog post \ud83d\ude42 This blog post shows how you can create a simple ToDo application and associate ToDoes with Users from ASP.NET Identity. In other words, this post shows how you can mix and match Entity Framework Code First Models for the application specific data with the Models of User from ASP.NET Identity.<\/font><\/p>\n<p> <font size=\"2\"><\/font>  <\/p>\n<h2>Let us define the requirements of this application :-<\/h2>\n<p> <font size=\"2\"><\/font>  <\/p>\n<ul><font size=\"2\"><\/font>    <\/p>\n<li><font size=\"2\">Only authenticated users should be able to create, edit, delete and see their own ToDoes.<\/font><\/li>\n<p>   <font size=\"2\"><\/font>    <\/p>\n<li><font size=\"2\">Users should not be able to view or edit ToDoes which were created by other users. D\u2019Oh you would be thinking that this should be a given, but I figured I should mention that since this is what I hit while writing this sample.<\/font><\/li>\n<p>   <font size=\"2\"><\/font>    <\/p>\n<li><font size=\"2\">Only Users who belong to Admin Role (I am going to call this User as Admin User in this post) should be authorized to see all the ToDoes in the application. This means that the Admin User can see the ToDoes for all the users. This requirement will highlight the authorization requirements mentioned in the comment.<\/font><\/li>\n<p>   <font size=\"2\"><\/font><\/ul>\n<p> <font size=\"2\"><\/font>  <\/p>\n<h2>Let us see how we can build an application using ASP.NET Identity which fulfills these requirements<\/h2>\n<p><font size=\"2\">I am going to write this post as a tutorial as well and use the ASP.NET Project templates in Visual Studio 2013 so hopefully it would be easy to follow and try out and later on you can implement this flow in your own application.<\/font><\/p>\n<p> <font size=\"2\"><\/font>  <\/p>\n<ul>\n<ul>\n<li>\n<h4><font size=\"3\">Create a File \u2013 New ASP.NET Project and select ASP.NET MVC with Individual User Accounts. <\/font><\/h4>\n<\/li>\n<\/ul>\n<\/ul>\n<ul><font size=\"2\"><\/font>    <\/p>\n<ul><font size=\"2\"><\/font>      <\/p>\n<li><font size=\"2\">This project creates an application where a user can login by registering an account with the website or use Social Login providers such as Facebook, Twitter etc. For the purpose of this example we will register a user with the website.<\/font><\/li>\n<p>     <font size=\"2\"><\/font>      <\/p>\n<li><strong><font size=\"3\">Initialize ASP.NET Identity to create Admin User and Admin Role and Add Admin User to Admin Role<\/font><\/strong><\/li>\n<p>     <font size=\"2\"><\/font>      <\/p>\n<ul><font size=\"2\"><\/font>        <\/p>\n<li><font size=\"2\">You can initialize ASP.NET Identity when the application starts. Since ASP.NET Identity is Entity Framework based in this sample,&#160; you can create DatabaseInitializer which is configured to get called each time the app starts.<\/font><\/li>\n<p>       <font size=\"2\"><\/font>        <\/p>\n<li><font size=\"2\"><strong>Set the Database Initializer in Global.asax<\/strong><\/font><\/li>\n<\/ul>\n<\/ul>\n<\/ul>\n<ul>\n<ul>\n<ul>\n<ul>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span>Database.SetInitializer&lt;MyDbContext&gt;(<span class=\"kwrd\">new<\/span> MyDbInitializer());<\/pre>\n<\/p><\/div>\n<p>        <font size=\"2\"><\/font><\/ul>\n<p>      <font size=\"2\"><\/font><\/ul>\n<\/ul>\n<\/ul>\n<p><font size=\"2\"><strong><\/strong><\/font><\/p>\n<ul>\n<ul>\n<ul>\n<li><font size=\"2\"><strong>Initialize the database to create Admin Role and Admin User<\/strong><\/font><\/li>\n<ul>\n<li><font size=\"2\">In Line 5 we are creating a UserManger from ASP.NET Identity system which will let us do operations on the User such as Create, List, Edit and Verify the user. You can think of the UserManager as being analogus to SQLMembershpProvider in ASP.NET 2.0<\/font><\/li>\n<li><font size=\"2\">In Line 6, we are creating a RoleManager from ASP.NET Identity system which lets us operate on Roles. You can think of the RoleManager as being analogus to SQLRoleMembershpProvider in ASP.NET 2.0<\/font><\/li>\n<li><font size=\"2\">In this example, my User type is called in MyUser. In the project template, it is called ApplicationUser.<\/font><\/li>\n<\/ul>\n<\/ul>\n<\/ul>\n<\/ul>\n<p><font size=\"2\"><\/font>&#160;<\/p>\n<ul>\n<ul>\n<ul><font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span><span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> MyDbInitializer : DropCreateDatabaseAlways&lt;MyDbContext&gt;<\/pre>\n<pre><span class=\"lnum\">   2:  <\/span>    {<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   3:  <\/span>        <span class=\"kwrd\">protected<\/span> <span class=\"kwrd\">override<\/span> <span class=\"kwrd\">void<\/span> Seed(MyDbContext context)<\/pre>\n<pre><span class=\"lnum\">   4:  <\/span>        {<\/pre>\n<pre class=\"alt\"><p><span class=\"lnum\">   5:  <\/span>            var UserManager = <span class=\"kwrd\">new<\/span> UserManager&lt;MyUser&gt;(<span class=\"kwrd\">new<\/span> <\/p><p>                                                UserStore&lt;MyUser&gt;(context)); <\/p><\/pre>\n<pre><span class=\"lnum\">   6:  <\/span>            var RoleManager = <span class=\"kwrd\">new<\/span> RoleManager&lt;IdentityRole&gt;(<span class=\"kwrd\">new<\/span> <\/pre>\n<pre>                                          RoleStore&lt;IdentityRole&gt;(context));<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   7:  <\/span>&#160;<\/pre>\n<pre><span class=\"lnum\">   8:  <\/span>            <span class=\"kwrd\">string<\/span> name = <span class=\"str\">&quot;Admin&quot;<\/span>;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   9:  <\/span>            <span class=\"kwrd\">string<\/span> password = <span class=\"str\">&quot;123456&quot;<\/span>;<\/pre>\n<pre>&#160;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  13:  <\/span>&#160;<\/pre>\n<pre><span class=\"lnum\">  14:  <\/span>            <span class=\"rem\">\/\/Create Role Admin if it does not exist<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  15:  <\/span>            <span class=\"kwrd\">if<\/span> (!RoleManager.RoleExists(name))<\/pre>\n<pre><span class=\"lnum\">  16:  <\/span>            {<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  17:  <\/span>                var roleresult = RoleManager.Create(<span class=\"kwrd\">new<\/span> IdentityRole(name));<\/pre>\n<pre><span class=\"lnum\">  18:  <\/span>            }<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  19:  <\/span>&#160;<\/pre>\n<pre><span class=\"lnum\">  20:  <\/span>            <span class=\"rem\">\/\/Create User=Admin with password=123456<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  21:  <\/span>            var user = <span class=\"kwrd\">new<\/span> MyUser();<\/pre>\n<pre><span class=\"lnum\">  22:  <\/span>            user.UserName = name;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  23:  <\/span>            var adminresult = UserManager.Create(user, password);<\/pre>\n<pre><span class=\"lnum\">  24:  <\/span>&#160;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  25:  <\/span>            <span class=\"rem\">\/\/Add User Admin to Role Admin<\/span><\/pre>\n<pre><span class=\"lnum\">  26:  <\/span>            <span class=\"kwrd\">if<\/span> (adminresult.Succeeded)<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  27:  <\/span>            {<\/pre>\n<pre><span class=\"lnum\">  28:  <\/span>                var result = UserManager.AddToRole(user.Id, name);<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  29:  <\/span>            }<\/pre>\n<pre><span class=\"lnum\">  30:  <\/span>            <span class=\"kwrd\">base<\/span>.Seed(context);<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  31:  <\/span>        }<\/pre>\n<pre><span class=\"lnum\">  32:  <\/span>    }<\/pre>\n<\/p><\/div>\n<p>        <font size=\"2\"><\/font><\/ul>\n<p>      <font size=\"2\"><\/font><\/ul>\n<p>    <font size=\"2\"><\/font><\/ul>\n<\/ul>\n<p><font size=\"2\"><strong><\/strong><\/font><\/p>\n<ul>\n<ul>\n<li><font size=\"2\"><strong>Create an Entity Framework Code First ToDo model in ModelsAppModels,cs.<\/strong><\/font><\/li>\n<p>    <font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<li><font size=\"2\">Since we are using Entity Framework Code First, EF will create the right keys between Users and ToDo table.<\/font><\/li>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span><span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> MyUser : IdentityUser<\/pre>\n<pre><span class=\"lnum\">   2:  <\/span>    {<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   3:  <\/span>        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">string<\/span> HomeTown { get; set; }<\/pre>\n<pre><span class=\"lnum\">   4:  <\/span>        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">virtual<\/span> ICollection&lt;ToDo&gt;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   5:  <\/span>                             ToDoes { get; set; }<\/pre>\n<pre><span class=\"lnum\">   6:  <\/span>    }<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   7:  <\/span>&#160;<\/pre>\n<pre><span class=\"lnum\">   8:  <\/span>    <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> ToDo<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   9:  <\/span>    {<\/pre>\n<pre><span class=\"lnum\">  10:  <\/span>        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">int<\/span> Id { get; set; }<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  11:  <\/span>        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">string<\/span> Description { get; set; }<\/pre>\n<pre><span class=\"lnum\">  12:  <\/span>        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">bool<\/span> IsDone { get; set; }<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  13:  <\/span>        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">virtual<\/span> MyUser User { get; set; }<\/pre>\n<pre><span class=\"lnum\">  14:  <\/span>    }<\/pre>\n<\/p><\/div>\n<\/ul>\n<\/ul>\n<\/ul>\n<p><font size=\"2\"><\/font><\/p>\n<ul>\n<ul>\n<li><font size=\"2\"><strong>Use Scaffolding to generate a ToDo MVC controller along with Views to create, update, delete and list all the ToDoes.<\/strong> Follow this tutorial on how to use Scaffolding in ASP.NET <\/font><a href=\"http:\/\/www.asp.net\/visual-studio\/overview\/2013\/aspnet-scaffolding-overview\"><font size=\"2\">http:\/\/www.asp.net\/visual-studio\/overview\/2013\/aspnet-scaffolding-overview<\/font><\/a><\/li>\n<p>    <font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<li><font size=\"2\">Please ensure that you reuse the existing database context so you can store ASP.NET Identity and ToDoes in the same database. This is a convenient way of managing application data and membership data. You DbContext should look something like this<\/font><\/li>\n<p>      <font size=\"2\"><\/font><\/p>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span>    <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> MyDbContext : IdentityDbContext&lt;MyUser&gt;<\/pre>\n<pre><span class=\"lnum\">   2:  <\/span>    {<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   3:  <\/span>        <span class=\"kwrd\">public<\/span> MyDbContext()<\/pre>\n<pre><span class=\"lnum\">   4:  <\/span>            : <span class=\"kwrd\">base<\/span>(<span class=\"str\">&quot;DefaultConnection&quot;<\/span>)<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   5:  <\/span>        {<\/pre>\n<pre><span class=\"lnum\">   6:  <\/span>        }<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   7:  <\/span>&#160;<\/pre>\n<pre><span class=\"lnum\">   8:  <\/span>        <span class=\"kwrd\">protected<\/span> <span class=\"kwrd\">override<\/span> <span class=\"kwrd\">void<\/span> OnModelCreating(DbModelBuilder modelBuilder)<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   9:  <\/span>        {<\/pre>\n<pre><span class=\"lnum\">  10:  <\/span>        <span class=\"kwrd\">public<\/span> System.Data.Entity.DbSet&lt;AspnetIdentitySample.Models.ToDo&gt; <\/pre>\n<pre>                     ToDoes { get; set; }<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  11:  <\/span>    }<\/pre>\n<\/p><\/div>\n<p>      <font size=\"2\"><\/font><\/ul>\n<p>    <font size=\"2\"><\/font><\/ul>\n<\/ul>\n<p><font size=\"2\"><\/font><\/p>\n<ul>\n<ul>\n<li><strong><font size=\"3\">Only Authenticated Users should be able to List, Create, Update and Delete ToDoes<\/font><\/strong><\/li>\n<p>    <font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<li><font size=\"2\">To protect the ToDoes we will use the existing [Authorize] that existed in MVC4.&#160; For more information please visit <\/font><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.web.mvc.authorizeattribute(v=vs.108).aspx\"><font size=\"2\">http:\/\/msdn.microsoft.com\/en-us\/library\/system.web.mvc.authorizeattribute(v=vs.108).aspx<\/font><\/a><\/li>\n<p>      <font size=\"2\"><\/font><\/p>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span>[Authorize]<\/pre>\n<pre><span class=\"lnum\">   2:  <\/span> <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> ToDoController : Controller<\/pre>\n<\/p><\/div>\n<p>      <font size=\"2\"><\/font><\/ul>\n<p>    <font size=\"2\"><\/font><\/ul>\n<\/ul>\n<p><font size=\"2\"><\/font><\/p>\n<ul>\n<ul>\n<li><font size=\"2\"><strong>Update the generated ToDo controller to associate a User with ToDoes. <\/strong>In this sample I am only going to show you Create and list, but you can follow the similar pattern for Edit and Detele<\/font><\/li>\n<p>    <font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<li><font size=\"2\">Add an overload of ToDo controller constructor which takes in a UserManager for ASP.NET Identity. UserManager allows you to manage the User in ASP.NET Identity system.<\/font><\/li>\n<p>      <font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span>        <span class=\"kwrd\">private<\/span> MyDbContext db;<\/pre>\n<pre><span class=\"lnum\">   2:  <\/span>        <span class=\"kwrd\">private<\/span> UserManager&lt;MyUser&gt; manager;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   3:  <\/span>        <span class=\"kwrd\">public<\/span> ToDoController()<\/pre>\n<pre><span class=\"lnum\">   4:  <\/span>        {<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   5:  <\/span>            db = <span class=\"kwrd\">new<\/span> MyDbContext();<\/pre>\n<pre><span class=\"lnum\">   6:  <\/span>            manager = <span class=\"kwrd\">new<\/span> UserManager&lt;MyUser&gt;(<span class=\"kwrd\">new<\/span> UserStore&lt;MyUser&gt;(db));<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   7:  <\/span>        }<\/pre>\n<pre><span class=\"lnum\">   8:  <\/span>        <\/pre>\n<\/p><\/div>\n<p>        <font size=\"2\"><\/font><\/ul>\n<p>      <font size=\"2\"><\/font><\/ul>\n<\/ul>\n<\/ul>\n<p><font size=\"2\"><\/font><\/p>\n<ul>\n<ul>\n<ul>\n<li><font size=\"2\"><strong>Update Create Action<\/strong><\/font><\/li>\n<p>      <font size=\"2\"><\/font><\/p>\n<ul>\n<li><font size=\"2\">When you create a ToDo, we look up the logged in User in the ASP.NET Identity and associate the User object with the ToDoes. <\/font><\/li>\n<\/ul>\n<p>      <font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span>        <span class=\"kwrd\">public<\/span> async Task&lt;ActionResult&gt; Create<\/pre>\n<pre><span class=\"lnum\">   2:  <\/span>        ([Bind(Include=<span class=\"str\">&quot;Id,Description,IsDone&quot;<\/span>)] ToDo todo)<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   3:  <\/span>        {<\/pre>\n<pre><span class=\"lnum\">   4:  <\/span>            var currentUser = await manager.FindByIdAsync<\/pre>\n<pre>                                                 (User.Identity.GetUserId()); <\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   5:  <\/span>            <span class=\"kwrd\">if<\/span> (ModelState.IsValid)<\/pre>\n<pre><span class=\"lnum\">   6:  <\/span>            {<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   7:  <\/span>                todo.User = currentUser;<\/pre>\n<pre><span class=\"lnum\">   8:  <\/span>                db.ToDoes.Add(todo);<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   9:  <\/span>                await db.SaveChangesAsync();<\/pre>\n<pre><span class=\"lnum\">  10:  <\/span>                <span class=\"kwrd\">return<\/span> RedirectToAction(<span class=\"str\">&quot;Index&quot;<\/span>);<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  11:  <\/span>            }<\/pre>\n<pre><span class=\"lnum\">  12:  <\/span>&#160;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  13:  <\/span>            <span class=\"kwrd\">return<\/span> View(todo);<\/pre>\n<pre><span class=\"lnum\">  14:  <\/span>        }<\/pre>\n<\/p><\/div>\n<p>        <font size=\"2\"><\/font><\/ul>\n<p>&#160;&#160;&#160;&#160;&#160;&#160; <\/ul>\n<\/ul>\n<\/ul>\n<ul>\n<ul>\n<ul>\n<li><font size=\"2\"><strong>Update List Action<\/strong><\/font><\/li>\n<p>      <font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<li><font size=\"2\">We will only get the list of ToDoes created by the current logged in User<\/font><\/li>\n<p>        <font size=\"2\"><\/font><\/p>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span>        <span class=\"kwrd\">public<\/span> ActionResult Index()<\/pre>\n<pre><span class=\"lnum\">   2:  <\/span>        {<\/pre>\n<pre class=\"alt\"><p><span class=\"lnum\">   3:  <\/span>            var currentUser = manager.FindById(User.Identity.GetUserId());<\/p><\/pre>\n<pre><span class=\"lnum\">   4:  <\/span>            <span class=\"kwrd\">return<\/span> View(db.ToDoes.ToList().Where(<\/pre>\n<pre>                                   todo =&gt; todo.User.Id == currentUser.Id));<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   5:  <\/span>        }<\/pre>\n<\/p><\/div>\n<p>        <font size=\"2\"><\/font><\/ul>\n<p>      <font size=\"2\"><\/font><\/ul>\n<\/ul>\n<\/ul>\n<p><font size=\"2\"><\/font><\/p>\n<ul>\n<ul>\n<ul><\/ul>\n<li><strong><font size=\"3\">Allow only Admins to view the ToDoes for all the users<\/font><\/strong><\/li>\n<ul><font size=\"2\"><\/font><\/p>\n<ul><font size=\"2\"><\/font><\/p>\n<li><font size=\"2\">We will add a new Action in the ToDo controller to list all the ToDoes, but we will only authorize Users in Role Admin to have access to these ToDoes. Again we are using the same [Authorize] attribute that we used before<\/font><\/li>\n<p>        <font size=\"2\"><\/font><\/p>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span>        [Authorize(Roles=<span class=\"str\">&quot;Admin&quot;<\/span>)]<\/pre>\n<pre><span class=\"lnum\">   2:  <\/span>        <span class=\"kwrd\">public<\/span> async Task&lt;ActionResult&gt; All()<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   3:  <\/span>        {<\/pre>\n<pre><span class=\"lnum\">   4:  <\/span>            <span class=\"kwrd\">return<\/span> View(await db.ToDoes.ToListAsync());<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   5:  <\/span>        }<\/pre>\n<\/p><\/div>\n<p>        <font size=\"2\"><\/font><\/ul>\n<p>      <font size=\"2\"><\/font><\/ul>\n<p>    <font size=\"2\"><\/font><\/ul>\n<p>  <font size=\"2\"><\/font><\/ul>\n<p><strong><font size=\"3\"><\/font><\/strong><\/p>\n<ul>\n<ul>\n<li><strong><font size=\"3\">Add support to view the User details from the ToDo table<\/font><\/strong><\/li>\n<ul>\n<ul>\n<li><font size=\"3\">Since we associate the ToDoes with a User object, the benefit that we get is when we get the list of ToDoes for all the users, we can easily access any profile specific data for the user from the ToDo model itself. For eg. when the Admin views all the ToDoes we can also get a profile property for the user if we added any. In this sample we have added HomeTowm so we ill display HomeTown next to the ToDoes. <\/font><\/li>\n<li><font size=\"3\">The markup for the views looks like as follows<\/font><\/li>\n<ul>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span>@model IEnumerable<span class=\"kwrd\">&lt;<\/span><span class=\"html\">AspnetIdentitySample.Models.ToDo<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">   2:  <\/span>&#160;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   3:  <\/span>@{<\/pre>\n<pre><span class=\"lnum\">   4:  <\/span>    ViewBag.Title = &quot;Index&quot;;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   5:  <\/span>}<\/pre>\n<pre><span class=\"lnum\">   6:  <\/span>&#160;<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   7:  <\/span><span class=\"kwrd\">&lt;<\/span><span class=\"html\">h2<\/span><span class=\"kwrd\">&gt;<\/span>List of ToDoes for all Users<span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">h2<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">   8:  <\/span><span class=\"kwrd\">&lt;<\/span><span class=\"html\">p<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   9:  <\/span>    Notice that we can see the User info (UserName) and profile info such as HomeTown for the user as well.<\/pre>\n<pre><span class=\"lnum\">  10:  <\/span>    This was possible because we associated the User object with a ToDo object and hence<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  11:  <\/span>    we can get this rich behavior.<\/pre>\n<pre><span class=\"lnum\">  12:  <\/span><span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">p<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  13:  <\/span>&#160;<\/pre>\n<pre><span class=\"lnum\">  14:  <\/span><span class=\"kwrd\">&lt;<\/span><span class=\"html\">table<\/span> <span class=\"attr\">class<\/span><span class=\"kwrd\">=&quot;table&quot;<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  15:  <\/span>    <span class=\"kwrd\">&lt;<\/span><span class=\"html\">tr<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  16:  <\/span>        <span class=\"kwrd\">&lt;<\/span><span class=\"html\">th<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  17:  <\/span>            @Html.DisplayNameFor(model =<span class=\"kwrd\">&gt;<\/span> model.Description)<\/pre>\n<pre><span class=\"lnum\">  18:  <\/span>        <span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">th<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  19:  <\/span>        <span class=\"kwrd\">&lt;<\/span><span class=\"html\">th<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  20:  <\/span>            @Html.DisplayNameFor(model =<span class=\"kwrd\">&gt;<\/span> model.IsDone)<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  21:  <\/span>        <span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">th<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  22:  <\/span>        <span class=\"kwrd\">&lt;<\/span><span class=\"html\">th<\/span><span class=\"kwrd\">&gt;<\/span>@Html.DisplayNameFor(model =<span class=\"kwrd\">&gt;<\/span> model.User.UserName)<span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">th<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  23:  <\/span>        <span class=\"kwrd\">&lt;<\/span><span class=\"html\">th<\/span><span class=\"kwrd\">&gt;<\/span>@Html.DisplayNameFor(model =<span class=\"kwrd\">&gt;<\/span> model.User.HomeTown)<span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">th<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  24:  <\/span>    <span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">tr<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  25:  <\/span>&#160;<\/pre>\n<pre><span class=\"lnum\">  26:  <\/span>    @foreach (var item in Model)<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  27:  <\/span>    {<\/pre>\n<pre><span class=\"lnum\">  28:  <\/span>        <span class=\"kwrd\">&lt;<\/span><span class=\"html\">tr<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  29:  <\/span>            <span class=\"kwrd\">&lt;<\/span><span class=\"html\">td<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  30:  <\/span>                @Html.DisplayFor(modelItem =<span class=\"kwrd\">&gt;<\/span> item.Description)<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  31:  <\/span>            <span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">td<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  32:  <\/span>            <span class=\"kwrd\">&lt;<\/span><span class=\"html\">td<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  33:  <\/span>                @Html.DisplayFor(modelItem =<span class=\"kwrd\">&gt;<\/span> item.IsDone)<\/pre>\n<pre><span class=\"lnum\">  34:  <\/span>            <span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">td<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  35:  <\/span>            <span class=\"kwrd\">&lt;<\/span><span class=\"html\">td<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  36:  <\/span>                @Html.DisplayFor(modelItem =<span class=\"kwrd\">&gt;<\/span> item.User.UserName)<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  37:  <\/span>            <span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">td<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  38:  <\/span>            <span class=\"kwrd\">&lt;<\/span><span class=\"html\">td<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  39:  <\/span>                @Html.DisplayFor(modelItem =<span class=\"kwrd\">&gt;<\/span> item.User.HomeTown)<\/pre>\n<pre><span class=\"lnum\">  40:  <\/span>            <span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">td<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  41:  <\/span>        <span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">tr<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">  42:  <\/span>    }<\/pre>\n<pre class=\"alt\"><span class=\"lnum\">  43:  <\/span>&#160;<\/pre>\n<pre><span class=\"lnum\">  44:  <\/span><span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">table<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<\/p><\/div>\n<\/ul>\n<\/ul>\n<\/ul>\n<\/ul>\n<\/ul>\n<ul>\n<ul>\n<li><strong>Update the Layout Page to add links for ToDoes<\/strong><\/li>\n<ul>\n<div class=\"csharpcode\">\n<pre class=\"alt\"><span class=\"lnum\">   1:  <\/span><span class=\"kwrd\">&lt;<\/span><span class=\"html\">li<\/span><span class=\"kwrd\">&gt;<\/span>@Html.ActionLink(&quot;ToDo&quot;, &quot;Index&quot;, &quot;ToDo&quot;)<span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">li<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre><span class=\"lnum\">   2:  <\/span><span class=\"kwrd\">&lt;<\/span><span class=\"html\">li<\/span><span class=\"kwrd\">&gt;<\/span>@Html.ActionLink(&quot;ToDo for User In Role Admin&quot;, &quot;All&quot;, &quot;ToDo&quot;)<span class=\"kwrd\">&lt;\/<\/span><span class=\"html\">li<\/span><span class=\"kwrd\">&gt;<\/span><\/pre>\n<pre class=\"alt\"><span class=\"lnum\">   3:  <\/span>                    <\/pre>\n<\/p><\/div>\n<\/ul>\n<li><strong>Run the application<\/strong><\/li>\n<ul>\n<li>When you run the application you will the links at the top of the page as follows<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/10\/2553.image_thumb_72005D2F.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/10\/2553.image_thumb_72005D2F.png\" width=\"606\" height=\"104\" \/><\/a> <\/li>\n<\/ul>\n<\/ul>\n<\/ul>\n<ul>\n<ul>\n<li><strong><font size=\"3\">Create ToDo as a normal user (non admin)<\/font><\/strong><\/li>\n<ul>\n<ul>\n<li>Click ToDo and you will be redirected to Login page since you are not authenticated<\/li>\n<li>You can register a new account and create a ToDo<\/li>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/10\/7356.image_thumb_31CA43B5.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/10\/7356.image_thumb_31CA43B5.png\" width=\"412\" height=\"324\" \/><\/a> <\/li>\n<\/ul>\n<\/ul>\n<ul>\n<li>Once you create the ToDo, then you can view the ToDoes for yourself. Note you cannot view ToDoes for all users<\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/10\/1411.image_thumb_1F817CF3.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/10\/1411.image_thumb_1F817CF3.png\" width=\"632\" height=\"214\" \/><\/a> <\/li>\n<\/ul>\n<\/ul>\n<li><strong><font size=\"3\">View the ToDoes for all the users (admin only access)<\/font><\/strong><\/li>\n<ul>\n<li><font size=\"3\">Click the link \u201cToDo for User in Role Admin\u201d. you will be redirected back to the login page since you are not an Admin and hence you are not authorized to view this page<\/font><\/li>\n<li><font size=\"3\">Logout from this application and Login using the Admin you created when you were initializing the ASP.NET Identity system. User = Admin Password=123456<\/font><\/li>\n<li><font size=\"3\">Once you login you can view the ToDoes for all users<\/font><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/10\/0250.image_thumb_5AD4E2B1.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2013\/10\/0250.image_thumb_5AD4E2B1.png\" width=\"705\" height=\"232\" \/><\/a> <\/li>\n<\/ul>\n<\/ul>\n<\/ul>\n<h2>Conclusion<\/h2>\n<p>I hope you will find this walkthrough useful. For a completed sample you can visit my project at <a href=\"https:\/\/github.com\/rustd\/AspnetIdentitySample\">https:\/\/github.com\/rustd\/AspnetIdentitySample<\/a>. If you have any questions around ASP.NET Identity, please feel free to leave comments, ask them on asp.net\/forums of stackoverflow. I can also be reached on twitter (@rustd)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello everyone. I was prompted to write this post from a comment I received on http:\/\/blogs.msdn.com\/b\/webdev\/archive\/2013\/10\/17\/announcing-release-of-asp-net-and-web-tools-for-visual-studio-2013.aspx. I am pasting the comment verbatim from the post \u201cI&#8217;m having a lot of difficulty integrating the IdentityDbContext with other DbContexts to create a comprehensive model and database for my applications. If the membership system always stood on its [&hellip;]<\/p>\n","protected":false},"author":408,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[],"class_list":["post-1134","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet"],"acf":[],"blog_post_summary":"<p>Hello everyone. I was prompted to write this post from a comment I received on http:\/\/blogs.msdn.com\/b\/webdev\/archive\/2013\/10\/17\/announcing-release-of-asp-net-and-web-tools-for-visual-studio-2013.aspx. I am pasting the comment verbatim from the post \u201cI&#8217;m having a lot of difficulty integrating the IdentityDbContext with other DbContexts to create a comprehensive model and database for my applications. If the membership system always stood on its [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/408"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=1134"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/1134\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=1134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=1134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=1134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}