Code Monkey home page Code Monkey logo

okta-aspnet-mvc-example's Introduction

ASP.NET MVC + Okta

This example shows how to use Okta, OpenID Connect, and ASP.NET MVC 4.x+.

You can follow the quickstart for this project to see how it was created.

Prerequisites: Visual Studio and Windows.

Okta has Authentication and User Management APIs that reduce development time with instant-on, scalable user infrastructure. Okta's intuitive API and expert support make it easy for developers to authenticate, manage and secure users and roles in any application.

Getting started

To install this example application, clone this repository with Git:

git clone https://github.com/oktadeveloper/okta-aspnet-mvc-example.git

Or download a zip archive of the repository from GitHub and extract it on your machine.

Create an application in Okta

You will need to create an application in Okta to perform authentication.

Log in to your Okta Developer account (or sign up if you don’t have an account) and navigate to Applications > Add Application. Click Web, click Next, and give the app a name you’ll remember.

Change the Base URI to:

http://localhost:8080/

Change the Login redirect URI to:

http://localhost:8080/authorization-code/callback

Click Done. On the General Settings screen, click Edit.

Check Implicit (Hybrid) and Allow ID Token. Add a Logout redirect URI:

http://localhost:8080/Account/PostLogout

Note: You can run the project in Visual Studio to see the port it is assigned on your machine. It may be different than 8080. In that case, you'll need to update the URIs in Okta.

Scroll to the bottom of the Okta application page to find the client ID and client secret. You'll need those values in the next step.

Project configuration

Open the Web.config file and update these values:

  • okta:ClientId - The client ID of the Okta application
  • okta:ClientSecret - The client secret of the Okta application
  • okta:OrgUri - Replace {yourOktaDomain} with your Okta domain, found at the top-right of the Dashboard page.

Note: The value of {yourOktaDomain} should be something like dev-123456.oktapreview.com. Make sure you don't include -admin in the value!

Start the application

Use Visual Studio to run the project. It should start up on http://localhost:8080. (If it doesn't, update the URLs in Okta and in Web.config)

Links

Help

Please post any questions on the Okta Developer Forums. You can also email [email protected] if you would like to create a support ticket.

License

Apache 2.0, see LICENSE.

okta-aspnet-mvc-example's People

Contributors

laura-rodriguez avatar nbarbettini avatar raphaellondner-okta avatar sguryev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

okta-aspnet-mvc-example's Issues

self-hosted-login okta-aspnet-mvc-example giving RequireNonce error

I get this error

IDX21323: RequireNonce is 'System.Boolean'. OpenIdConnectProtocolValidationContext.Nonce was null, OpenIdConnectProtocol.ValidatedIdToken.Payload.Nonce was not null. The nonce cannot be validated. If you don't need to check the nonce, set OpenIdConnectProtocolValidator.RequireNonce to 'false'. Note if a 'nonce' is found it will be evaluated.

GetConfigurationAsync().Result can cause Deadlock

GetConfigurationAsync().Result can cause Deadlock

Starting suggestion: https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html
In short, never use .Result
See what this says:
https://weblogs.asp.net/pglavich/asp-net-web-api-request-response-usage-logging
" Here we are accessing the Result property from an asynchronous task in an attempt to make this code procedural and work in a synchronous manner. It is an easy thing to do, and looks like it makes sense. However, do not do this.
Generally, you should never access the ‘Result’ property of a Task unless you know that the task has completed as this can cause deadlocks in ASP.Net. Yes it is true.Sometimes this code may work but, if you don’t want to waste hours debugging deadlock issues in ASP.Net, I would advise against it. "

No default on issuer

Out of the box there is no "default" available on an Okta instance.
To get the example working we changed the following:

web.config
line 16: change
from [add key="okta:Issuer" value="https://{yourOktaDomain}.com/oauth2/default" /]
to [add key="okta:Issuer" value="https://{yourOktaDomain}.com" /]

Because out of the box the url oauth2/default/.well-known/openid-configuration is not available in an Okta instance

startup.cs change
line: 72 add "/oauth2" before the "/v1/token"
line: 80 add "/oauth2" before the "/v1/token"

Write-lock potential to create request bottleneck

The below code periodically places an external web request inside the scope of a write lock. If there is a delay at connecting to Okta there is potential that requests could just stack up waiting for the lock to free up. I recommend moving line 91 to the top of the method to prevent this.

https://github.com/oktadeveloper/okta-oauth-aspnet-codeflow/blob/7f029019d71918375cfb4d00bf1bffa3fee58d52/Api/OpenIdConnectCachingSecurityTokenProvider.cs#L86-L99

What is authorization-code/callback

@nbarbettini

In an MVC app we have (created by folloiwng the Okta sampels walkthru) we see the config file has:

        <add key="okta:RedirectUri" value="http://localhost:50583/authorization-code/callback" />

But we don't understand what this is, someone else asked about this too on the Okta forum but nobody has answered his question:

https://devforum.okta.com/t/what-is-authorization-code-callback-in-github-net-sample/3989

Could someone please explain what this is and what that URL means from a code standpoint?

Thanks

/authorization-code/callback Not Found

I setup the okta app for this code on my org, updated the web.config and ran the project as it is in the guide (readme.md). If I try to click login it gives me a 401 (Unauthorized) which is expected. Then what I do, is to login into my okta and then click on my configured web app and this redirects me to http://localhost:8080/authorization-code/callback, so bare with me here for a second: should I add a new controller to support the /authorization-code/callback action or this is done by owin? I am baffled on how this should work and if this is something missing in the sample code.

thanks for your feedback.

Login Feature Missing

In step #4 shows to --- Click on "Sign in with OpenID Connect" and sign in with your test user Okta credentials.

That navigation lands on /Callback/Tokens where the view shows Identity token, Access Token, and Refresh Token.

There is no login page, is something missing in this sample?

Include SDK

We should include the .NET SDK and give some examples of how it can be used alongside OpenID Connect.

View is referred to incorrectly

In the CodeFlow CallBackController, the return:
return View("Token", response);
should be
return View("Tokens", response);

UWP Sample Client

I'd like to request a UWP sample client that call the same API for this solution. I think that would be really helpful.

APICall authorization error

In the sample the MVC application is working with Okta authentication. After getting the token when I try API call, it is failing with the following error:

An exception of type 'System.IdentityModel.Tokens.SecurityTokenSignatureKeyNotFoundException' occurred in System.IdentityModel.Tokens.Jwt.dll but was not handled in user code

Additional information: IDX10500: Signature validation failed. Unable to resolve SecurityKeyIdentifier: 'SecurityKeyIdentifier
(
IsReadOnly = False,
Count = 1,
Clause[0] = System.IdentityModel.Tokens.NamedKeySecurityKeyIdentifierClause
)
',

SSL certificate required

Please add to Readme.md point about required SSL certificate to run the project. I didn't see any pop-up window in VisualStudio to generate such one. You will not be able to retrieve project URL without a signed certificate. To fix that just need to generate self-signed certificates for project addresses. This is the answer at stackoverflow which worked for me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.