Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

.NET Core v3 doesnt have UseIdentity() when building ApplicationBuidler in StartUp.cs

$
0
0

I upgraded & ran into this issue where `UseIdentity()` is not available in StartUp.cs when building application (ApplicationBuilder).

The webservice use it so what is the recommended fix?  The MVC Controller have attribute that checked for valid Identity.  The `OnAuthorizationAsync` in Filter Attribute class do check for active Identity.  This is where custom basic authentication pass in the login.

// Startup.cs

public void Configure(IApplicationBuilder applicationBuilder, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)

{

            // Webservice API access.

            applicationBuilder.UseWhen(context =>

                !context.Request.Path.StartsWithSegments(new PathString("/api/v1")), 

                b => b.UseIdentity()

            );

            applicationBuilder.UseWhen(context => 

                !context.Request.Path.StartsWithSegments(new PathString("/api/v1")), 

                b => b.UseAuthentication()

            );

        }

// MerchantsV1Controller.cs

[TypeFilter(typeof(AuthorizeWithNoChallengeFilterAttribute))]

public class MerchantsV1Controller : Controller

{

}

// AuthorizeWithNoChallengeFilterAttribute.cs

public class AuthorizeWithNoChallengeFilterAttribute : IAsyncAuthorizationFilter

{

     public async Task OnAuthorizationAsync(AuthorizationFilterContext context)

     {

            if (!context.HttpContext.User.Identity.IsAuthenticated)

                context.Result = new UnauthorizedResult();

                await Task.CompletedTask;

        }

    }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>