<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>asp net user authentication &#8211; ASP.NET Hosting Reviews and Guides</title>
	<atom:link href="https://topreviewhostingasp.net/tag/asp-net-user-authentication/feed/" rel="self" type="application/rss+xml" />
	<link>https://topreviewhostingasp.net</link>
	<description>ASP.NET Hosting &#124; Reviews &#124; Tips &#38; Tutorial</description>
	<lastBuildDate>Mon, 20 Mar 2023 07:47:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://topreviewhostingasp.net/wp-content/uploads/2017/01/cropped-trhaico-32x32.png</url>
	<title>asp net user authentication &#8211; ASP.NET Hosting Reviews and Guides</title>
	<link>https://topreviewhostingasp.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to Use ASP.NET Identity in a Console Apps</title>
		<link>https://topreviewhostingasp.net/how-to-use-asp-net-identity-in-a-console-apps/</link>
					<comments>https://topreviewhostingasp.net/how-to-use-asp-net-identity-in-a-console-apps/#respond</comments>
		
		<dc:creator><![CDATA[Jacques Hunt]]></dc:creator>
		<pubDate>Mon, 20 Mar 2023 07:46:02 +0000</pubDate>
				<category><![CDATA[Hosting Tips]]></category>
		<category><![CDATA[asp net]]></category>
		<category><![CDATA[asp net identity]]></category>
		<category><![CDATA[asp net tips]]></category>
		<category><![CDATA[asp net tutorial]]></category>
		<category><![CDATA[asp net user authentication]]></category>
		<guid isPermaLink="false">https://topreviewhostingasp.net/?p=3478</guid>

					<description><![CDATA[Your apps may implement authentication quite easily with the help of Microsoft&#8217;s ASP.NET framework. Hardly little has to be done to create a standard web application. You need to do little more than tick a few boxes to proceed. You may not be aware, though, that ASP.NET user authentication may be added to console applications. [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Your apps may implement authentication quite easily with the help of Microsoft&#8217;s ASP.NET framework. Hardly little has to be done to create a standard web application. You need to do little more than tick a few boxes to proceed.</p>



<p>You may not be aware, though, that ASP.NET user authentication may be added to console applications.</p>



<p>Thankfully, the hidden magic is mostly gone with.NET Core. All the components needed for authentication are available to you, and you are free to reuse them as necessary.</p>



<p>Making two projects—a console application and a web application—is the simplest method to do this. Make sure ASP.NET authentication is turned on in the web application before you begin.</p>



<p>All that is left to do is transfer some settings from the web application to our console application.</p>



<p>We must first include a few NuGet packages in our console application. References will be required for:</p>



<ul>
<li>Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore</li>
<li>Microsoft.AspNetCore.Identity.EntityFrameworkCore</li>
<li>Microsoft.AspNetCore.Identity.UI</li>
</ul>


<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://www.asphostportal.com" target="_blank" rel="noreferrer noopener"><img fetchpriority="high" decoding="async" width="300" height="271" class="wp-image-2584 aligncenter" src="https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01.png" alt="" srcset="https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01.png 300w, https://topreviewhostingasp.net/wp-content/uploads/2018/11/ahp-banner-aspnet-01-50x45.png 50w" sizes="(max-width: 300px) 100vw, 300px" /></a></figure></div>


<p>You must first construct your NuGet packages before creating the ApplicationUser class and the Entity Framework database context.</p>



<pre class="wp-block-code"><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
{
    public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)
        : base(options)
    {

    }

    public DbSet&lt;ApplicationUser&gt; ApplicationUsers { get; set; } 
}</code></pre>



<pre class="wp-block-code"><code>public class ApplicationUser: IdentityUser
{

}</code></pre>



<p>After getting your Entity Framework stuff set up, all that is really left to configure is your .NET Core DI.</p>



<pre class="wp-block-code"><code>public class Configurator
{
    public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
            options.UseSqlite(
                configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()
            .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
            .AddDefaultTokenProviders();

        services.AddLogging();

        services.AddScoped(
            typeof(IAuthentication),
            typeof(Authentication));
    }
}</code></pre>



<p>Now that everything has been set up, we can use our console application to access ASP.NET Identity data just as we would in a web application. A example authentication service with numerous typical user-related features is shown below.</p>



<pre class="wp-block-code"><code>public class Authentication : IAuthentication
    {
        readonly UserManager&lt;ApplicationUser&gt; _userManager;

        public Authentication(UserManager&lt;ApplicationUser&gt; userManager)
        {
            _userManager = userManager;
        }
        
        public async Task&lt;User&gt; CreateUser(string email, string password)
        {
            var applicationUser = new ApplicationUser()
            {
                Email = email,
                UserName = email,
            };

            
            var result = await _userManager.CreateAsync(applicationUser, password);
            if (!result.Succeeded)
            {
                return null;
            }
            
            var loaded = await _userManager.FindByNameAsync(email);
            return DTOMapper.Map&lt;User&gt;(loaded);
        }
        
        public async Task&lt;User&gt; UpdatePassword(string email, string oldPassword, string newPassword)
        {
            var loaded1 = await _userManager.FindByNameAsync(email);
            if (loaded1 == null)
            {
                return null;
            }
            
            var result = await _userManager.ChangePasswordAsync(loaded1, oldPassword, newPassword);
            if (!result.Succeeded)
            {
                return null;
            }
            
            var loaded2 = await _userManager.FindByNameAsync(email);
            return DTOMapper.Map&lt;User&gt;(loaded2);
        }
        
        public async Task&lt;bool&gt; CheckPassword(string email, string password)
        {
            var loaded1 = await _userManager.FindByNameAsync(email);
            if (loaded1 == null)
            {
                return false;
            }
            
            return await _userManager.CheckPasswordAsync(loaded1, password);
        }
    }</code></pre>
]]></content:encoded>
					
					<wfw:commentRss>https://topreviewhostingasp.net/how-to-use-asp-net-identity-in-a-console-apps/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
