Easy Steps to Integrate ASOS in ASP.NET Core API | Syncfusion Blogs
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)

Easy Steps to Integrate ASOS in ASP.NET Core API

ASOS (AspNet.Security.OpenIdConnect.Server) is an advanced OAuth2 for ASP.NET Core 1.x and 2.x. In this article, we explain the integration process of ASOS, corresponding to client_credentials and password grant types, to enable:

Token-based authentication

Token-based authentication is the process of creating a token and attaching it with a HTTP request, which will be made to access an API. If a valid token is attached, then the request will be allowed. If an invalid token is attached, then the request will be rejected.

This is the type of authentication that will work while calling with client_credentials grant type.

Prerequisites

  • Client ID for API
  • Client secret ID for API

Startup.cs configure services

services.AddAuthentication().AddOpenIdConnectServer(options =>
{
    options.AllowInsecureHttp = true;
    options.AccessTokenLifetime = TimeSpan.FromMinutes(60); //Provide token expiry here.
    options.TokenEndpointPath = "/token"; //Provide token end point path here.
    options.Provider.OnValidateTokenRequest = context =>
    {
        //["ClientCredentials:ClientId"] denotes your API client id in the format of string.
        //["ClientCredentials:ClientSecret"] denotes your API client secret id in the format of string.
        if (context.ClientId == ["ClientCredentials:ClientId"] && context.ClientSecret == ["ClientCredentials:ClientSecret"])
        {
            context.Validate();
        }
        else
        {
            context.Reject(
                        error: OpenIdConnectConstants.Errors.InvalidClient,
                        description: "Invalid Client details");
        }
        return Task.CompletedTask;
    };
});
services.AddAuthentication(OAuthValidationDefaults.AuthenticationScheme).AddOAuthValidation();

User-based authentication

This type of authentication will work while calling with a password of grant type.

Prerequisites

You should have user login credentials to allow users to access a particular API request.

Startup.cs configure services

options.Provider.OnHandleTokenRequest = context =>
{
    if (!string.IsNullOrEmpty(context.Request.Username) && !string.IsNullOrEmpty(context.Request.Password) && context.Request.IsPasswordGrantType())
    {
        bool loginValidation = GetLoginvalidation(context.Request.Username, context.Request.Password);
        if (!loginValidation)
        {
            context.Reject(
            error: OpenIdConnectConstants.Errors.InvalidGrant,
            description: loginValidation);

            return Task.CompletedTask;
        }
        else 
        {
            // If user information is correct, you can do customized changes like adding in claims in this block based on your requirement.
        }
    }
};

Additional points to be considered

  • Add UseAuthentication() to make the previous authentication work. Add this above app.UseMvc() in the configure method in startup.cs.
  • The controller of each request should have the authorized filter to authorize each request.
[Authorize]
public JsonResult Login()
{
  return;
}

Conclusion

In this blog, we have seen the integration process of ASOS in ASP.NET Core API to enable token-based authentication and user-based authentication.

Syncfusion provides 70+ ASP.NET Core UI controls such as DataGrid, Charts, and Scheduler. You can use them to speed up your application development.

If you have any questions, please let us know in the comments section below. You can contact us through our support forum, Direct-Trac, or Feedback Portal. We are waiting to hear your feedback!

Tags:

Share this post:

Comments (2)

Hi Priyanka, Why OpenIdConnectServer isn’t available for .net core 3.1 ?. Actually i’m trying to migrate one of project to core 3.1 and i’m stuck here. This is the code which i want to add in startup.cs =>
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString(“/Token”),
Provider = new AppOAuthProvider(PublicClientId),
AuthorizeEndpointPath = new PathString(“/Account/ExternalLogin”),
AccessTokenExpireTimeSpan = TimeSpan.FromHours(4),
AllowInsecureHttp = true //Don’t do this in production ONLY FOR DEVELOPING: ALLOW INSECURE HTTP!

};

It would be very helpful if you share some solution to overcome this problem. Thanks in advance.

Hi Sagar,

Please install the middleware named AspNet.Security.OpenIdConnect.Server in your .NET Core 3.1 project.
Once installed you can use same code mentioned in this blog.

If you face any issue after this middleware installation, please share us the error details.
Please let us know if any concern.

Regards,
Priyanka S.

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed