Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Τάσος Κοντός 1 post 21 karma points
    Oct 27, 2023 @ 14:18
    Τάσος Κοντός
    0

    Configure Asp .Net Identity in Umbraco 12 project

    Hello i am trying to learn .net and umbraco so excuse my lack of knowledge. I am trying to configure umrbaco with asp .net identity that will be help in a seperate schema in my database.

    As of now i have made an extention method that is called from ConfigureServices in startup

     public void ConfigureServices(IServiceCollection services)
     {
         services.AddUmbraco(_env, _config)
             .AddBackOffice()
             .AddWebsite()
             .AddDeliveryApi()
             .AddComposers()
             .AddCustomServices()
             .Build();
     }
    

    Inside the extention method is where i am trying to configure asp.net identity, like this:

    public static class UmbracoBuilderExtentions
    {
        public static IUmbracoBuilder AddCustomServices(this IUmbracoBuilder umbracoBuilder) {
    
            // Register your Identity DbContext here
            umbracoBuilder.Services.AddDbContext<AuthenticationDbContext>(options =>
            {
                options.UseSqlServer("myconnectionstring");
            });
    
            umbracoBuilder.Services.AddIdentityCore<AuthenticationUser>()
                 .AddRoles<IdentityRole>().AddEntityFrameworkStores<AuthenticationDbContext>()
                 .AddTokenProvider<DataProtectorTokenProvider<AuthenticationUser>>(TokenOptions.DefaultProvider);
    
            umbracoBuilder.Services.PostConfigure<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme,
            options =>
            {
                options.LoginPath = "/login";
                options.AccessDeniedPath = "/access-denied";
            });
    
            umbracoBuilder.Services.AddAuthentication(x =>
            {
                x.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                x.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
                x.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            })
            .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme);
    
            //umbracoBuilder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
            //.AddCookie();
    
            umbracoBuilder.Services.AddScoped<UserManager<AuthenticationUser>>();
            umbracoBuilder.Services.AddScoped<SignInManager<AuthenticationUser>>();
    
            return umbracoBuilder;
        }
    }
    

    i have tried several iterations but havent managed to make it work. While i am able to register users successfully i cant manage to successfully sign in ever!

    this is my sign in method for reference:

    private async Task<bool> SignIn(AuthenticationUser user, string password)
    {
        var result = await _signInManager.CheckPasswordSignInAsync(user, password, true);
    
        if (result.Succeeded)
        {
            await _signInManager.SignInAsync(user, isPersistent: false);
            return true;
        }
        return result.Succeeded;
    }
    

    any suggestions on how i can make it work as intended?

  • Angelo 20 posts 72 karma points
    Dec 25, 2023 @ 04:39
    Angelo
    0

    Did you manage?

Please Sign in or register to post replies

Write your reply to:

Draft