aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Core/ActiveDirectoryAuthenticationProvider.cs
blob: 91ea57817fe18b146b76a320f5c9615c35e92877 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Core
{
    public class ActiveDirectoryAuthProvider : SqlAuthenticationProvider
    {
        public override async Task<SqlAuthenticationToken> AcquireTokenAsync(SqlAuthenticationParameters parameters)
        {
            AuthenticationContext authContext = new AuthenticationContext(parameters.Authority);
            authContext.CorrelationId = parameters.ConnectionId;
            AuthenticationResult result;

            result = await authContext.AcquireTokenAsync(
                parameters.Resource,
                "ec612854-7abc-457b-808a-5d0c5ba80c57",
                new Uri("http://www.google.com"),
                new PlatformParameters(PromptBehavior.Auto),
                new UserIdentifier(
                    parameters.UserId,
                    UserIdentifierType.RequiredDisplayableId));

            return new SqlAuthenticationToken(result.AccessToken, result.ExpiresOn);
        }

        public override bool IsSupported(SqlAuthenticationMethod authenticationMethod)
        {
            return authenticationMethod == SqlAuthenticationMethod.ActiveDirectoryInteractive;
        }
    }
}