Base solution for your next web application
Open Closed

Automatic registration and login #5370


User avatar
0
tom.ohle created

I want to achieve the following feature in my ASP.NET Zero app (.NET Core & Angular 6).

Assume this is the first time I have accessed the app (ie. no user has been created for me yet).

  1. Turn on my computer.
  2. Log into Windows.
  3. Browse to the app in Chrome.
  4. Start using the app (ie. no login or registration required).

After accessing the app for the first time, a user should be created for me with the default roles assigned (ie. just like what happens when logging in for the first time via the ASP.NET Zero LDAP feature).

To be clear, I have already successfully tested using my Windows credentials to log in as a first time user after enabling LDAP, but I want to skip the login process completely by using the fact that I have already authenticated myself when I logged into the Windows operating system.

How can I achieve this with ASP.NET Zero?


3 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team
  • User Avatar
    0
    tom.ohle created

    Thanks! I had to make a few changes, so this is how I did it.

    "angular\src\account\login\login.component.ts"

    class LoginComponent {    
        ngOnInit() {
          this.loginService.authenticateModel.userNameOrEmailAddress = 'foo';
          this.loginService.authenticateModel.password = 'bar';
          this.login();
        }
      }
    

    "aspnet-core\src\ProjectName.Core\Authentication\AlwaysTrue\AlwaysTrueExternalAuthSource.cs"

    public class AlwaysTrueExternalAuthSource: DefaultExternalAuthenticationSource<Tenant, User>, ITransientDependency
      {
        public override string Name => "AlwaysTrueExternalAuthSource";
    
        public override Task<bool> TryAuthenticateAsync(string userNameOrEmailAddress, string plainPassword, Tenant tenant)
        {
          return Task.FromResult(true);
        }
      }
    

    "aspnet-core\src\ProjectName.Core\ProjectNameCoreModule.cs"

    public class ProjectNameCoreModule : AbpModule
      {
        public override void PreInitialize()
        {
          Configuration.Modules.Zero().UserManagement.ExternalAuthenticationSources.Add<AlwaysTrueExternalAuthSource>();
        }
      }
    

    "aspnet-core\src\ProjectName.Web.Core\Controllers\TokenAuthController.cs"

    public class TokenAuthController : ProjectNameControllerBase
      {
        [HttpPost]
        public async Task<AuthenticateResultModel> Authenticate([FromBody] AuthenticateModel model)
        {
          var windowsIdentity = WindowsIdentity.GetCurrent();
          model.UserNameOrEmailAddress = windowsIdentity.Name.ToLowerInvariant().Replace("\\","");
          
          var loginResult = await GetLoginResultAsync(...)
        }
      }
    
  • User Avatar
    0
    aaron created
    Support Team

    That's great :)