Jerry
Jerry

Reputation: 6557

.Net Core 3 UseOpenIdConnectAuthentication

We have a legacy .net 4 web application. We use AAD to authenticate a user. Then we used the below code to run immediately after authentication to do things like get a users data or configure permissions.

We also already have user and role tables that are maintained.

Now, we have to re-creating this application using ASP.net Core 3. We also want to keep our existing user and role tables.

1: What is the equivalent of "UseOpenIdConnectAuthentication" for executing code after a user has been authenticated? In .net core 3?

2: How can we add a claim based on the authenticated user in .net core 3?

--startup.cs for legacy .Net 4 app
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
...

app.UseOpenIdConnectAuthentication(
    new OpenIdConnectAuthenticationOptions
    {
        ClientId = clientId,
        Authority = authority,
        RedirectUri = redirectUri,
        ...

        Notifications = new OpenIdConnectAuthenticationNotifications
            {
                AuthenticationFailed = OnAuthenticationFailed,
                SecurityTokenValidated = notification =>
                {
                    //user is authenticated
                    //Do stuff here to set their permissions, get data, etc...

Upvotes: 0

Views: 371

Answers (1)

Tiny Wang
Tiny Wang

Reputation: 15906

Does this what you want?

services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(options =>
                {
                    options.Events.OnTokenValidated = async context =>
                    {
                        options.TokenValidationParameters.ValidAudiences = new List<string> { "aud1"};
                    };
                });

Upvotes: 0

Related Questions