Giter Site home page Giter Site logo

sample-adh-authentication_authorization-dotnet's Introduction

Authorization Code Flow + PKCE DotNet Sample and Test

Version: 1.4.6

Build Status

This client uses the OAuth2/OIDC Authorization Code Flow + PKCE to obtain an access token. See the main ADH Authentication samples page README for more information about this flow.

Requirements

  • .NET 6.0 (this requires Visual Studio 17.1 or later)

  • Web Browser with Javascript enabled

    • You will need Google Chrome if you want to run the automated test
  1. Ensure that the client contains $"{RedirectHost}:{RedirectPort}/{RedirectPath}" in the list of RedirectUris
  • Default value from config is: https://127.0.0.1:54567/signin-oidc
  • You can change the values to match your preferences

Running the sample

Prerequisites

  • Register an Authorization Code client in ADH and ensure that the registered client in ADH contains https://127.0.0.1:54567/signin-oidc in the list of RedirectUris.
  • Configure the sample using the file appsettings.placeholder.json. Before editing, rename this file to appsettings.json. This repository's .gitignore rules should prevent the file from ever being checked in to any fork or branch, to ensure credentials are not compromised.
  • Replace the placeholders in the appsettings.json file with your Tenant Id and Client Id obtained from registration. The username and password fields are used for testing and can be left as is.
  • Note: As a test, a request is made against the users endpoint. If the tenant being used has strict mode enabled and the client does not have Tenant Admin permissions, the test will fail. In this case, it is recommended to change the url to a type or a stream that the client has permission to access.

Using Visual Studio

  1. Load the .csproj in this directory
  2. Rebuild project
  3. Run it
    • If you want to see the token and other outputs from the program, put a breakpoint at the end of the main method and run in debug mode
  4. Follow the prompts in the web browser to log in
    • Keep in mind that if you are already logged in with the same Account in the browser, you will not have to log in again
  5. Return to the application after having been authenticated in the browser

Using Command Line

  • Make sure you have the install location of dotnet added to your path
  • Run the following command from the location of this project:
dotnet run
  • Follow the prompts in the web browser to log in
  • Return to the application after having been authenticated in the browser

Running the automated test

Test Prerequisites

  • Make sure Google Chrome is the default browser on your test system.
  • Download the ChromeDriver version from http://chromedriver.storage.googleapis.com/index.html corresponding to the version of Google Chrome that is installed. Set the environmental variable ChromeWebDriver to the directory containing the Chrome Driver executable.
  • Update the appsettings.json file with the username and password for the Microsoft account that will be used to log in. The test is only written to work with a personal Microsoft account and must only prompt for only username followed by password (no Two-Factor authentication or other consent or informational prompts). Also if the location of the sample application has been modified then change the RedirectHost and/or RedirectPort.

Test Using Visual Studio

  • Load the .csproj from the AuthorizationCodeFlowTest directory above this in Visual Studio
  • Rebuild project
  • Open Test Explorer and make sure there is one test called Test1 showing
  • Run the test

Test Using Command Line

  • Make sure you have the install location of dotnet added to your path
  • Run the following command from the location of the AuthorizationCodeFlowTest project (you may need to run as Administrator for the test to use the Chrome Driver):
dotnet test

Tested against DotNet 6.0

For the main ADH Authentication samples page ReadMe
For the main ADH samples page ReadMe
For the main AVEVA samples page ReadMe

sample-adh-authentication_authorization-dotnet's People

Contributors

anton-dahlin avatar cbardini avatar dependabot[bot] avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

praveen-irvine

sample-adh-authentication_authorization-dotnet's Issues

Sample is not working: "Invalid redirect_uri"

Hello AVEVA Team,

I tried to use your sample code.
I updated the appsettings.json with the correct information.

Then, when I try to run your code, I obtain the following on the terminal:

PS C:\Dev\sample-adh-authentication_authorization-dotnet-main> dotnet run --project "c:\Dev\sample-adh-authentication_authorization-dotnet-main\AuthorizationCodeFlow\AuthorizationCodeFlow.csproj"
+-----------------------+
|  Sign in with OIDC    |
+-----------------------+

Prompting for login via a browser...

This is what the browser is displaying:

image

This seems to be related to the Redirection URL, but I have kept the default one:

  "RedirectHost": "https://127.0.0.1",
  "RedirectPort": "54567",
  "RedirectPath": "signin-oidc",

and I put this redirect URL on my AVEVA Connect instance:

image

I tried to do it manually in Golang using https://github.com/coreos/go-oidc and I obtain the exact same error :(.
I assume something has changed between the current implementation of AVEVA Connect and your sample code. Can you point me out to where I should look to make it work?

Thanks,

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.