/// <summary>
        /// This method gets called when the user uses an external identity provider to authenticate.
        /// </summary>
        /// <param name="externalUser">The external user.</param>
        /// <returns>
        /// The authentication result.
        /// </returns>
        public virtual Task <AuthenticateResult> AuthenticateExternalAsync(ExternalIdentity externalUser)
        {
            var query =
                from u in _users
                where
                u.Provider == externalUser.Provider &&
                u.ProviderId == externalUser.ProviderId
                select u;

            var user = query.SingleOrDefault();

            if (user == null)
            {
                var name = externalUser.Claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Name);
                if (name == null)
                {
                    return(Task.FromResult <AuthenticateResult>(null));
                }

                user = new InMemoryUser
                {
                    Subject    = CryptoRandom.CreateUniqueId(),
                    Provider   = externalUser.Provider,
                    ProviderId = externalUser.ProviderId,
                    Username   = name.Value,
                    Claims     = externalUser.Claims
                };
                _users.Add(user);
            }

            var p      = IdentityServerPrincipal.Create(user.Subject, GetDisplayName(user), Constants.AuthenticationMethods.External, user.Provider);
            var result = new AuthenticateResult(p);

            return(Task.FromResult(result));
        }
        public Task<ExternalAuthenticateResult> AuthenticateExternalAsync(string subject, Models.ExternalIdentity externalUser)
        {
            var query =
                from u in _users
                where
                    u.Provider == externalUser.Provider.Name &&
                    u.ProviderId == externalUser.ProviderId
                select u;
            
            var user = query.SingleOrDefault();
            if (user == null)
            {
                var name = externalUser.Claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Name);
                if (name == null)
                {
                    return Task.FromResult<ExternalAuthenticateResult>(null);
                }
                var claims = externalUser.Claims.Except(new Claim[] { name });

                user = new InMemoryUser
                {
                    Subject = Guid.NewGuid().ToString("N"),
                    Provider = externalUser.Provider.Name,
                    ProviderId = externalUser.ProviderId,
                    Username = name.Value,
                    Claims = claims.ToArray()
                };
                _users.Add(user);
            }

            return Task.FromResult(new ExternalAuthenticateResult(user.Provider, user.Subject, user.Username));
        }
        public static IEnumerable<InMemoryUser> Get()
        {
            var users = new InMemoryUser[]
            {
                new InMemoryUser{Subject = "818727", Username = "******", Password = "******", 
                    Claims = new Claim[]
                    {
                        new Claim(Constants.ClaimTypes.Name, "Alice Smith"),
                        new Claim(Constants.ClaimTypes.GivenName, "Alice"),
                        new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
                        new Claim(Constants.ClaimTypes.Email, "*****@*****.**"),
                    }
                },
                new InMemoryUser{Subject = "88421113", Username = "******", Password = "******", 
                    Claims = new Claim[]
                    {
                        new Claim(Constants.ClaimTypes.Name, "Bob Smith"),
                        new Claim(Constants.ClaimTypes.GivenName, "Bob"),
                        new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
                        new Claim(Constants.ClaimTypes.Email, "*****@*****.**"),
                    }
                },
            };

            return users;
        }
Esempio n. 4
0
        public Task <ExternalAuthenticateResult> AuthenticateExternalAsync(string subject, Models.ExternalIdentity externalUser)
        {
            var query =
                from u in _users
                where
                u.Provider == externalUser.Provider.Name &&
                u.ProviderId == externalUser.ProviderId
                select u;

            var user = query.SingleOrDefault();

            if (user == null)
            {
                var name = externalUser.Claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Name);
                if (name == null)
                {
                    return(Task.FromResult <ExternalAuthenticateResult>(null));
                }
                var claims = externalUser.Claims.Except(new Claim[] { name });

                user = new InMemoryUser
                {
                    Subject    = Guid.NewGuid().ToString("N"),
                    Provider   = externalUser.Provider.Name,
                    ProviderId = externalUser.ProviderId,
                    Username   = name.Value,
                    Claims     = claims.ToArray()
                };
                _users.Add(user);
            }

            return(Task.FromResult(new ExternalAuthenticateResult(user.Provider, user.Subject, user.Username)));
        }
        protected virtual string GetDisplayName(InMemoryUser user)
        {
            var nameClaim = user.Claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Name);
            if (nameClaim != null)
            {
                return nameClaim.Value;
            }

            return user.Username;
        }
        /// <summary>
        /// Retrieves the display name.
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        protected virtual string GetDisplayName(InMemoryUser user)
        {
            var nameClaim = user.Claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Name);

            if (nameClaim != null)
            {
                return(nameClaim.Value);
            }

            return(user.Username);
        }
        public static IdentityServerServiceFactory Create(
                    string issuerUri, string siteName, string publicHostAddress = "")
        {
            var settings = new LocalTestCoreSettings(issuerUri, siteName, publicHostAddress);
            
            var codeStore = new InMemoryAuthorizationCodeStore();
            var tokenStore = new InMemoryTokenHandleStore();
            var consent = new InMemoryConsentService();
            var scopes = new InMemoryScopeService(LocalTestScopes.Get());
            var clients = new InMemoryClientService(LocalTestClients.Get());
            var logger = new TraceLogger();

            var users = new InMemoryUser[]
            {
                new InMemoryUser{Subject = "alice", Username = "******", Password = "******", 
                    Claims = new Claim[]
                    {
                        new Claim(Constants.ClaimTypes.GivenName, "Alice"),
                        new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
                        new Claim(Constants.ClaimTypes.Email, "*****@*****.**"),
                    }
                },
                new InMemoryUser{Subject = "bob", Username = "******", Password = "******", 
                    Claims = new Claim[]
                    {
                        new Claim(Constants.ClaimTypes.GivenName, "Bob"),
                        new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
                        new Claim(Constants.ClaimTypes.Email, "*****@*****.**"),
                    }
                },
            };
            var userSvc = new InMemoryUserService(users);

            var fact = new IdentityServerServiceFactory
            {
                Logger = () => logger,
                UserService = () => userSvc,
                AuthorizationCodeStore = () => codeStore,
                TokenHandleStore = () => tokenStore,
                CoreSettings = () => settings,
                ConsentService = () => consent,
                ScopeService = () => scopes,
                ClientService = () => clients
            };

            return fact;
        }
        public static IdentityServerServiceFactory Create(
                    string issuerUri, string siteName, string publicHostAddress = "")
        {
            var users = new InMemoryUser[]
            {
                new InMemoryUser{Subject = "818727", Username = "******", Password = "******", 
                    Claims = new Claim[]
                    {
                        new Claim(Constants.ClaimTypes.GivenName, "Alice"),
                        new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
                        new Claim(Constants.ClaimTypes.Email, "*****@*****.**"),
                    }
                },
                new InMemoryUser{Subject = "88421113", Username = "******", Password = "******", 
                    Claims = new Claim[]
                    {
                        new Claim(Constants.ClaimTypes.GivenName, "Bob"),
                        new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
                        new Claim(Constants.ClaimTypes.Email, "*****@*****.**"),
                    }
                },
            };

            var settings = new Settings(issuerUri, siteName, publicHostAddress);
            var scopes = new InMemoryScopeService(Scopes.Get());
            var clients = new InMemoryClientService(Clients.Get());
            var userSvc = new InMemoryUserService(users);

            var fact = new IdentityServerServiceFactory
            {
                UserService = Registration.RegisterFactory<IUserService>(() => userSvc),
                CoreSettings = Registration.RegisterFactory<CoreSettings>(() => settings),
                ScopeService = Registration.RegisterFactory<IScopeService>(() => scopes),
                ClientService = Registration.RegisterFactory<IClientService>(() => clients)
            };

            return fact;
        }
        /// <summary>
        /// This method gets called when the user uses an external identity provider to authenticate.
        /// </summary>
        /// <param name="externalUser">The external user.</param>
        /// <returns>
        /// The authentication result.
        /// </returns>
        public virtual Task<AuthenticateResult> AuthenticateExternalAsync(ExternalIdentity externalUser)
        {
            var query =
                from u in _users
                where
                    u.Provider == externalUser.Provider &&
                    u.ProviderId == externalUser.ProviderId
                select u;

            var user = query.SingleOrDefault();
            if (user == null)
            {
                var name = externalUser.Claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Name);
                if (name == null)
                {
                    return Task.FromResult<AuthenticateResult>(null);
                }

                user = new InMemoryUser
                {
                    Subject = CryptoRandom.CreateUniqueId(),
                    Provider = externalUser.Provider,
                    ProviderId = externalUser.ProviderId,
                    Username = name.Value,
                    Claims = externalUser.Claims
                };
                _users.Add(user);
            }

            var p = IdentityServerPrincipal.Create(user.Subject, GetDisplayName(user), Constants.AuthenticationMethods.External, user.Provider);
            var result = new AuthenticateResult(p);
            return Task.FromResult(result);
        }