How to implement oauth2 server in ASP.NET MVC 5 and WEB API 2 [closed] How to implement oauth2 server in ASP.NET MVC 5 and WEB API 2 [closed] asp.net asp.net

How to implement oauth2 server in ASP.NET MVC 5 and WEB API 2 [closed]


I also struggled finding articles on how to just generate the token part. I never found one and wrote my own. So if it helps:

The things to do are:

  • Create a new web application
  • Install the following NuGet packages:
    • Microsoft.Owin
    • Microsoft.Owin.Host.SystemWeb
    • Microsoft.Owin.Security.OAuth
    • Microsoft.AspNet.Identity.Owin
  • Add a OWIN startup class

Then create a HTML and a JavaScript (index.js) file with these contents:

var loginData = 'grant_type=password&username=test.test@mail.com&password=test123';var xmlhttp = new XMLHttpRequest();xmlhttp.onreadystatechange = function () {    if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {        alert(xmlhttp.responseText);    }}xmlhttp.open("POST", "/token", true);xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xmlhttp.send(loginData);
<!DOCTYPE html><html><head>    <title></title></head><body>    <script type="text/javascript" src="index.js"></script></body></html>

The OWIN startup class should have this content:

using System;using System.Security.Claims;using Microsoft.Owin;using Microsoft.Owin.Security.OAuth;using OAuth20;using Owin;[assembly: OwinStartup(typeof(Startup))]namespace OAuth20{    public class Startup    {        public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }        public void Configuration(IAppBuilder app)        {            OAuthOptions = new OAuthAuthorizationServerOptions()            {                TokenEndpointPath = new PathString("/token"),                Provider = new OAuthAuthorizationServerProvider()                {                    OnValidateClientAuthentication = async (context) =>                    {                        context.Validated();                    },                    OnGrantResourceOwnerCredentials = async (context) =>                    {                        if (context.UserName == "test.test@mail.com" && context.Password == "test123")                        {                            ClaimsIdentity oAuthIdentity = new ClaimsIdentity(context.Options.AuthenticationType);                            context.Validated(oAuthIdentity);                        }                    }                },                AllowInsecureHttp = true,                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1)            };            app.UseOAuthBearerTokens(OAuthOptions);        }    }}

Run your project. The token should be displayed in the pop-up.


I am researching the same thing and stumbled upon identityserver which implements OAuth and OpenID on top of ASP.NET. It integrates with ASP.NET identity and Membership Reboot with persistence support for Entity Framework.

So, to answer your question, check out their detailed document on how to setup an OAuth and OpenID server.