Security icon indicating copy to clipboard operation
Security copied to clipboard

Secure ASP.NET 5 Web Api

Open eqbalsajadi opened this issue 11 years ago • 12 comments

Hi guys,

How to login to ASP.NET 5 Web Api by WebClient class in a .NetFramework 2 Windows Forms? I have already done that for ASP.NET 4.5 using this article http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api But don't know how to do it for ASP.NET 5. It seems Authorization Server is not supported by it #83. Is there any alternative? In a nut shell, what is your solution to secure an ASP.NET 5 Web Api that is used by third party clients such as mobile or windows application?

Please help

eqbalsajadi avatar Jul 14 '15 19:07 eqbalsajadi

@PinpointTownes

eqbalsajadi avatar Jul 21 '15 06:07 eqbalsajadi

Well - MVC 6 supports OAuth2 bearer tokens (typically JWTs) - so nothing has really changed here.

The thing that has changed is that nothing is built-in anymore to produce those tokens. From my point of view the answer is identityserver which is an OAuth2 and OpenID Connect frameworks that supports all you are asking for and more. Start here: https://identityserver.github.io/Documentation/docs/overview/bigPicture.html

I am sure Kevin has something to add ;)

leastprivilege avatar Jul 21 '15 06:07 leastprivilege

@leastprivilege well, I think we already said everything in the previous tickets: https://github.com/aspnet/Security/issues/39 https://github.com/aspnet/Security/issues/83 :smile:

To sum up: IdentityServer is still your best option if you're looking for a turnkey solution/ecosystem, that natively integrates with ASP.NET Identity 2 (Katana's version, not vNext one) or MembershipReboot and that will eventually offer a ready-to-use interface to manage your client applications.

If you need dnxcore50 support, want to fully customize your authorization endpoint, or if you're simply looking for a much more low-level approach like the one offered by the OAuthAuthorizationServerMiddleware used in the tutorial mentioned in the original post, AspNet.Security.OpenIdConnect.Server definitely has the advantage.

FYI, @AlexZeitler and @shaunluttin are currently working on pretty cool samples you might be interested in:

  • https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/pull/104: it's a sample demonstrating how you can use an AngularJS client with the implicit flow. In this sample, users are redirected to the authorization server, where they can authenticate using either local authentication (username/password) or external authentication (Google, Twitter), before being redirected back to the JS client application with an identity token. It's similar to this existing sample (https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/tree/vNext/samples/Mvc) but with a JS client application instead of a MVC app.
  • https://github.com/aspnet-contrib/AspNet.Security.OpenIdConnect.Server/pull/98: this sample still uses a JS frontend, but relies on the "resource owner password credentials grant type" to offer username/password authentication without redirecting your users to the authorization server.

Good luck! :smile:

kevinchalet avatar Jul 21 '15 08:07 kevinchalet

yep that's a good summary.

Our samples are here: https://github.com/IdentityServer/IdentityServer3.Samples/tree/master/source/Clients

..and yes we are not running on the CoreCLR right now. But we will by the time ASP.NET will be released ;)

leastprivilege avatar Jul 21 '15 08:07 leastprivilege

..and yes we are not running on the CoreCLR right now. But we will by the time ASP.NET will be released ;)

Ah, it gives me far enough time to create new companion projects to make AspNet.Security.OpenIdConnect.Server a turnkey solution then :smile:

kevinchalet avatar Jul 21 '15 08:07 kevinchalet

Good luck! :)

leastprivilege avatar Jul 21 '15 09:07 leastprivilege

RequireSignedTokens = false,
ValidateIssuerSigningKey = false,
ValidateSignature = false

:smile:

kevinchalet avatar Jul 24 '15 09:07 kevinchalet

Since this issue gets referenced every time somebody asks about token issuance in ASP.NET 5 - here's a small walkthrough how to do it with identityserver:

http://leastprivilege.com/2015/07/22/the-state-of-security-in-asp-net-5-and-mvc-6-oauth-2-0-openid-connect-and-identityserver/

leastprivilege avatar Dec 23 '15 20:12 leastprivilege

Hi guys,

I am trying to use the OAuthMiddleware to authenticate against google. I have a google token that I am sending to a ASP.NET vNext Web API and I am trying to validate this token. On previous versions of ASP.NET I have been using this code to do this:

private static TokenValidationParameters GetTvp(List<X509Certificate2> certs)
{
    TokenValidationParameters tvp = new TokenValidationParameters();
    tvp.ValidIssuer = "accounts.google.com";
    tvp.ValidateIssuer = true;
    tvp.ValidateAudience = true;
    tvp.ValidateIssuerSigningKey = true;
    tvp.ValidateLifetime = true;

    var tokens = new List<SecurityToken>();
    var keys = new List<X509SecurityKey>();

    foreach (var certificate in certs)
    {
        X509SecurityToken certToken = new X509SecurityToken(certificate);

        var key = new X509SecurityKey(certificate);

        tokens.Add(certToken);
        keys.Add(key);
    }
    tvp.IssuerSigningKeyResolver = (a, b, c, d) =>
    {
        var key = keys.Where(x => b.ToString().ToLower().Contains(x.Certificate.Thumbprint.ToLower())).First();
        return key;
    };
    tvp.IssuerSigningTokens = tokens;
    tvp.IssuerSigningKeys = keys;

    return tvp;
}

but currently I can not find a "X509SecurityToken" implementation of System.IdentityModel.Tokens.SecurityToken in System.IdentityModel.Tokens v5.0.0-rc1-211161024. Do you have any suggestions how I can validate the google JWT token in vNext?

Sinstraliz avatar Jan 21 '16 12:01 Sinstraliz

@Sinstraliz X509SecurityToken has not been ported to IM 5. Instead, you need to use X509SecurityKey.

kevinchalet avatar Jan 21 '16 14:01 kevinchalet

Ok thank you for the quick response.

Sinstraliz avatar Jan 21 '16 14:01 Sinstraliz

Closing because in ASP.NET Core 2.2 we'll have a better story for auth in Web APIs.

Eilon avatar Sep 27 '18 21:09 Eilon