Aspnetcore.docs: Not working when trying this in an empty project with your own controllers and views

Created on 23 Jul 2018  Â·  4Comments  Â·  Source: dotnet/AspNetCore.Docs

As I tried to follow this guide, I'm left with more questions than I had before I started. This article is in dire need of a quality assurance review. I will explain why.

As you progress through the various steps, you will hit a snag once you add the Configure Microsoft Account Authentication. The example seems straight-forward enough, but if you've made the web application project as stipulated in the parent tutorial, you will have a serious problem trying to see how the "/logon-microsoft" (endpoint/callback/action/controller/whatever it might be) is set, configured, or handled. When I try to duplicate this in my own asp.net core 2.1 mvc app, it just won't work! Furthermore, I seem to find no code that explains how the callbacks are handled, tokens are distributed, and let alone how the logout is done. To clarify, I'm trying to decipher how this is done as I am unable to get it to work unless I base my webapp off of the tutorial page hierarchy.

The MVC hierarchy I use is the one outlined in the book Pro Entity Framework core 2 for ASP.NET Core MVC by Adam Freeman. I know it might not be the _ideal_ way to do it, but the way it's done in that book makes more sense than the official examples I've seen up to this point.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

P1 Source - Docs.ms

Most helpful comment

and changed primary key for the schema from a string to a Guid.

You don't need to change the PK.
This tutorial is out of date and the templates have changed. The old templates generated the IEmailSender implementation. See https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio#create-full-identity-ui-source

I'll increase the priority to get this updated.

All 4 comments

@Forumtroll We'll try to address this in the next doc sweep.

@Rick-Anderson Fantastic. I'll keep you updated with whatever I can find out example-wise and post it here.

Following step-by-step left me with a non-working app. The issue appears to be that the documentation follows examples of using the default scaffolding, but then expects the follower to have already written and ApplicationUser class and changed primary key for the schema from a string to a Guid.

To eventually get it to work:
I ended up changing my ApplicationDbContext, adding a Mock implementation of IEmailSender, swapping out RazorPage @inject for the SignInManager and UserManager and Deleting my migration data and recreating from scratch, because Update-Database wouldn't allow change of PK from String to Guid.

Here is the log of what I changed:
Make some user models (changing the account ID to GUID from String

namespace MyMVA_OAuth.Models
{
    // Add profile data for application users by adding properties to the ApplicationUser class
    public class ApplicationUser : IdentityUser<Guid>
    {
    }

    public class ApplicationRole : IdentityRole<Guid>
    {
    }


    public class MockEmailSender : IEmailSender
    {
        public Task SendEmailAsync(string email, string subject, string htmlMessage)
        {
            Console.WriteLine(htmlMessage);
            var toReturn = Task.FromResult(true);
            return toReturn;
            throw new NotImplementedException();
        }
    }
    }


Changed the Injection into RazorPages for the Login Partial to use my new objects rather than IdentityUser

@using Microsoft.AspNetCore.Identity
@using MyMVA_OAuth.Models

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

Change the StartUP to use my new classes and it needed an Email service DI

            // services.AddDefaultIdentity<IdentityUser>()
            //    .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddIdentity<ApplicationUser, ApplicationRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();

            // Add application services.
            services.AddTransient<IEmailSender, MockEmailSender>();

            services.AddAuthentication()
                .AddMicrosoftAccount(microsoftOptions =>
                {
                    microsoftOptions.ClientId = Configuration["Authentication:Microsoft:ApplicationId"];
                    microsoftOptions.ClientSecret = Configuration["Authentication:Microsoft:Password"];
                });

Changed ApplicationDbContext to take into consideration my new user and role objects

 //public class ApplicationDbContext : IdentityDbContext
    //{
    //    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
    //        : base(options)
    //    {
    //    }
    //}


    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid>
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            // Customize the ASP.NET Core Identity model and override the defaults if needed.
            // For example, you can rename the ASP.NET Core Identity table names and more.
            // Add your customizations after calling base.OnModelCreating(builder);
        }
    }

and changed primary key for the schema from a string to a Guid.

You don't need to change the PK.
This tutorial is out of date and the templates have changed. The old templates generated the IEmailSender implementation. See https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-2.1&tabs=visual-studio#create-full-identity-ui-source

I'll increase the priority to get this updated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

serpent5 picture serpent5  Â·  3Comments

AnthonyMastrean picture AnthonyMastrean  Â·  3Comments

YeyoCoder picture YeyoCoder  Â·  3Comments

Raghumu picture Raghumu  Â·  3Comments

sonichanxiao picture sonichanxiao  Â·  3Comments