title: Adding Redirection immediately after Login in ASP.Net Core 2.1 using Identity Core
body: <p>I am trying to achieve redirections immediately after Login in a .Net Core 2.1 application using Identity Core.</p>
  <p>The redirections are dependent on roles of the logged in user.</p>
  <p>I am getting a Null Reference exception.</p>
  <p>I read a few stack overflow questions and Git Issues and understood that this is because the user is not stored to the database right after sign in:</p>
  <pre><code>var result =await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true).Result;
  </code></pre>
  <p>I tried the following to retrieve the role of the logged in user:</p>
  <p>Method-1:</p>
  <pre><code>string userRole =_signInManager.Context.User.FindFirst(ClaimTypes.Role).Value;
  </code></pre>
  <p>Method-2:</p>
  <p>To determine if a user exists in a given role:</p>
  <pre><code>User.IsInRole(&quot;RoleName&quot;)
  </code></pre>
  <p>Method-3:</p>
  <pre><code>_userManager.GetClaimsAsync(user)
  </code></pre>
  <p>I am getting a Null reference exception in all cases;
  I understand this is because of the request not being completed.</p>
  <p>However I don't understand what is going wrong.</p>
  <p>Need direction.</p>
  <p>This my startup.cs:</p>
  <pre><code> public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          // This method gets called by the runtime. Use this method to add services to the container.
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  options.UseSqlServer(
                      Configuration.GetConnectionString(&quot;DefaultConnection&quot;)));
  
              services.AddIdentity&lt;IdentityUser,IdentityRole&gt;()
                  .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;();
  
              services.Configure&lt;CookiePolicyOptions&gt;(options =&gt;
              {
                  // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                  options.CheckConsentNeeded = context =&gt; true;
                  options.MinimumSameSitePolicy = SameSiteMode.None;
                  
              });
  
              services.ConfigureApplicationCookie(options =&gt;
              {
                  // Cookie settings  
                  options.Cookie.HttpOnly = true;
                  options.ExpireTimeSpan = TimeSpan.FromMinutes(30);
                  options.LoginPath = &quot;/Identity/Account/Login&quot;; 
                  options.LogoutPath = &quot;/Identity/Account/Logout&quot;; 
                  options.AccessDeniedPath = &quot;/Identity/Account/AccessDenied&quot;; 
                  options.SlidingExpiration = true;
              });
  
              services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
          
          }
  
          // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
          public void Configure(IApplicationBuilder app, IHostingEnvironment env)
          {
              if (env.IsDevelopment())
              {
                  app.UseDeveloperExceptionPage();
                  app.UseDatabaseErrorPage();
              }
              else
              {
                  app.UseExceptionHandler(&quot;/Home/Error&quot;);
                  app.UseHsts();
              }
  
              app.UseHttpsRedirection();
              app.UseStaticFiles();
              app.UseCookiePolicy();
  
              app.UseAuthentication();
  
              app.UseMvc(routes =&gt;
              {
                  routes.MapRoute(
                      name: &quot;default&quot;,
                      template: &quot;{Controller=Home}/{action=Index}/{id?}&quot;);
              });
          }
      }
  </code></pre>
  <p>Login - Page  controller of Identity core:</p>
  <pre><code>public async Task&lt;IActionResult&gt; OnPostAsync(string returnUrl = null)
  {
      returnUrl = returnUrl ?? Url.Content(&quot;return path&quot;);
      
      if (ModelState.IsValid)
      {
  
          var result = _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true).Result;
          if (result.Succeeded)
          {
              var usera = User.IsInRole(&quot;Role1&quot;);
              var users = User.IsInRole(&quot;Role2&quot;);
  
              //string userEmail = _signInManager.Context.User.FindFirst(ClaimTypes.Name).Value;
              //string userRole = _signInManager.Context.User.FindFirst(ClaimTypes.Role).Value;
              if (User.IsInRole(&quot;Admin&quot;))
              {
                  return RedirectToAction(&quot;path1&quot;);
              }
              else if (User.IsInRole(&quot;Supervisor&quot;))
              {
                 return RedirectToAction(&quot;path2&quot;);
              }
              else if (User.IsInRole(&quot;Member&quot;))
              {
                return RedirectToAction(&quot;path3&quot;);
              }
              else
              {
                   return RedirectToPage(&quot;/Identity/Account/AccessDenied&quot;);
              }
          }
          if (result.RequiresTwoFactor)
          {
              return RedirectToPage(&quot;./LoginWith2fa&quot;, new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
          }
          if (result.IsLockedOut)
          {
              _logger.LogWarning(&quot;User account locked out.&quot;);
              return RedirectToPage(&quot;./Lockout&quot;);
          }
          else
          {
              ModelState.AddModelError(string.Empty, &quot;Invalid login attempt.&quot;);
              return Page();
          }
      }
  
     
      return Page();
  }
  </code></pre>