7 Replies - 117 Views - Last Post: 17 January 2019 - 02:53 AM Rate Topic: -----

#1 andrewsw   User is online

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

publish Core app with default Identity authentication

Posted 11 January 2019 - 08:01 AM

I have an ASP.NET Core (Angular) application and am using the default Identity Authentication.

Having published to the web server, I can reach the login page at /Identity/Account/Login?ReturnUrl=%2F but I cannot register or login. There is a warning that appears in the browser's console,

Quote

This page includes a password or credit card input in a non-secure context. A warning has been added to the URL bar

but, being a warning, I think this is maybe a red herring. (Obviously, the warning will need to be resolved at some point.)

I cannot then visit other pages of the my site.

I suspect it is because the Identity database that was created for me locally is not recreated on the server, so there is no where for the login details to go. I believe that I need to execute the Migrations.

Is this something that know about? How can I execute the Identity Migrations on publishing?

The application came with a file IdentityHostingStartup.cs containing the below, maybe I need to add some code to it?

[assembly: HostingStartup(typeof(People.Areas.Identity.IdentityHostingStartup))]
namespace People.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDbContext<IdentityContext>(options =>
                    options.UseSqlServer(
                        context.Configuration.GetConnectionString("IdentityConnection")));

                services.AddDefaultIdentity<User>()
                    .AddEntityFrameworkStores<IdentityContext>();
            });
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: publish Core app with default Identity authentication

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,719
  • Joined: 12-June 08

Re: publish Core app with default Identity authentication

Posted 11 January 2019 - 08:17 AM

Hmm.. Hadn't need to use that auth yet, but maybe you are missing the 'addrole' from the docs?

https://docs.microso...=aspnetcore-2.2
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is online

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: publish Core app with default Identity authentication

Posted 11 January 2019 - 08:42 AM

Hello and thank you.

I think I'm missing a large amount of info and understanding about this Identity/authentication :dontgetit:

I thought I would be clever and just disable it in Startup,

                // TODO: reinstate the authentication
                //var policy = new AuthorizationPolicyBuilder()
                //                 .RequireAuthenticatedUser()
                //                 .Build();
                //config.Filters.Add(new AuthorizeFilter(policy));

so that at least the site could be viewed and explored, but this just led to a tonne of new, non-helpful, errors. 504, 500, blah, "Unknown Error".

It is frustrating having to do all this with zero guidance or assistance from my senior or colleagues, grrr.

Still, it's Friday afternoon.

Edit: I forgot to disable app.UseAuthentication(); in Configure and was briefly optimistic, but it still fails.
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: publish Core app with default Identity authentication

Posted 11 January 2019 - 09:09 AM

The Identity connection string is

Quote

"IdentityConnection": "Server=(localdb)\\mssqllocaldb;Database=People;Trusted_Connection=True;MultipleActiveResultSets=true",

When publishing should it point to my existing, non-localdb, server? That is, does (localdb) make sense when publishing?

If so, then perhaps I can create the database 'People' and then somehow run the migrations, perhaps via PowerShell, manually on the server?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 14691
  • View blog
  • Posts: 58,719
  • Joined: 12-June 08

Re: publish Core app with default Identity authentication

Posted 11 January 2019 - 09:20 AM

This is out of my depth at the moment, but I can poke at it a little later. I figure I should get familiar with it _some_ time..
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is online

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: publish Core app with default Identity authentication

Posted 11 January 2019 - 09:37 AM

Thank you.

I suppose it boils down to, with a new ASP.NET Core application, with the default Identity authentication scaffolded, it creates an Area 'Identity' containing context, IdentityHostingStartup.cs, etc., a default IdentityConnection with (localdb), and a Migrations folder.

As I recall the Identity migrations were initiated with

PM> Update-Database -context IdentityContext


Here is fragment from the initial migration:

namespace People.Migrations
{
    public partial class initial : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "AspNetRoles",
                columns: table => new
                {
                    Id = table.Column<string>(nullable: false),
                    Name = table.Column<string>(maxLength: 256, nullable: true),
                    NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
                    ConcurrencyStamp = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_AspNetRoles", x => x.Id);
                });


  • Does (localdb) make sense when publishing? Should it point to a blank database on the server?
  • Home are the migrations to be executed when published, to generate the Identity tables (and database if not present)?

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is online

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: publish Core app with default Identity authentication

Posted 17 January 2019 - 02:36 AM

Forgot to get back on this, but I did get it working.

I switched (localdb) to a database on the server, manually created this (empty) database.
I then also had to create a default user and password and give them read/write roles.

During this process I did try a few ways to execute migrations but, in the end, with a new connection and database, when I published, the blank database was filled with all the Identity tables and I was then able to register and login. The migrations had been executed.

[I noticed in the Publish options there where checkboxes to execute migrations, which is useful to know for the future.]



I believe that localdb is just intended for development. It is not like something like SQLite that can be packaged and distributed with the application. Besides, with a web application, the database(s) need to be accessible and maintainable on a proper server, not tucked away in an application directory.
Was This Post Helpful? 1
  • +
  • -

#8 andrewsw   User is online

  • dependency injected
  • member icon

Reputation: 6668
  • View blog
  • Posts: 27,308
  • Joined: 12-December 12

Re: publish Core app with default Identity authentication

Posted 17 January 2019 - 02:53 AM

This is also an Angular application. Authentication is applied via Startup.cs essentially via this code:

            services.AddMvc(config =>
            {
                // using Microsoft.AspNetCore.Mvc.Authorization;
                // using Microsoft.AspNetCore.Authorization;
                var policy = new AuthorizationPolicyBuilder()
                                 .RequireAuthenticatedUser()
                                 .Build();
                config.Filters.Add(new AuthorizeFilter(policy));
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

I have a link to Login, which works and then redirects back to the home page.

              <li>
                    <a href="/Identity/Account/Login?ReturnUrl=%2F" (click)='collapse()'>
                        <span class='glyphicon glyphicon-th-list'></span> Log in
                    </a>
                </li>


How can I set up communication between my Angular application and the Identity authentication to tell it "this geezer has logged in successfully, show him your stuff" and "this geezer aint logged in, redirect him to login and come back".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1