/// <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);
        }
Esempio n. 2
0
        public static List<InMemoryUser> Get()
        {
            var currentUser = new InMemoryUser
            {
                Username = "******",
                Password = "******",
                Subject = "1",
                Claims = new[]
                {
                    new Claim(Constants.ClaimTypes.GivenName, "Bob"),
                    new Claim(Constants.ClaimTypes.FamilyName, "Smith"),
                }
            };

            return new List<InMemoryUser>
            {
                currentUser
            };
        }
        /// <summary>
        /// This method gets called when the user uses an external identity provider to authenticate.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        public override Task AuthenticateExternalAsync(ExternalAuthenticationContext context)
        {
            var query =
                from u in _users
                where
                u.Provider == context.ExternalIdentity.Provider &&
                u.ProviderId == context.ExternalIdentity.ProviderId
                select u;

            var user = query.SingleOrDefault();

            if (user == null)
            {
                string displayName;

                var name = context.ExternalIdentity.Claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Name);
                if (name == null)
                {
                    displayName = context.ExternalIdentity.ProviderId;
                }
                else
                {
                    displayName = name.Value;
                }

                user = new InMemoryUser
                {
                    Subject    = CryptoRandom.CreateUniqueId(),
                    Provider   = context.ExternalIdentity.Provider,
                    ProviderId = context.ExternalIdentity.ProviderId,
                    Username   = displayName,
                    Claims     = context.ExternalIdentity.Claims
                };
                _users.Add(user);
            }

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

            context.AuthenticateResult = new AuthenticateResult(p);

            return(Task.FromResult(0));
        }
Esempio n. 4
0
        public static List<InMemoryUser> Get()
        {
            var firstUser = new InMemoryUser()
            {
                Username = "******",
                Password = "******",
                Subject = "1"
            };

            var secondUser = new InMemoryUser()
            {
                Username = "******",
                Password = "******",
                Subject = "2"
            };

            return new List<InMemoryUser>
            {
                firstUser,
                secondUser
            };
        }
		/// <summary>
		/// This method gets called when the user uses an external identity provider to authenticate.
		/// </summary>
		/// <param name="context">The context.</param>
		/// <returns></returns>
		public override Task AuthenticateExternalAsync(ExternalAuthenticationContext context)
		{
			var query =
				from u in _users
				where
					u.Provider == context.ExternalIdentity.Provider &&
					u.ProviderId == context.ExternalIdentity.ProviderId
				select u;

			var user = query.SingleOrDefault();
			if (user == null)
			{
				string displayName;

				var name = context.ExternalIdentity.Claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.Name);
				if (name == null)
				{
					displayName = context.ExternalIdentity.ProviderId;
				}
				else
				{
					displayName = name.Value;
				}

				user = new InMemoryUser
				{
					Subject = CryptoRandom.CreateUniqueId(),
					Provider = context.ExternalIdentity.Provider,
					ProviderId = context.ExternalIdentity.ProviderId,
					Username = displayName,
					Claims = context.ExternalIdentity.Claims
				};
				_users.Add(user);
			}

			// TEST CODE
			//List<Claim> claims = new List<Claim>()
			//{
			// new Claim(ClaimTypes.Upn, context.ExternalIdentity.ProviderId)
			//};

			context.AuthenticateResult = new AuthenticateResult(user.Subject, GetDisplayName(user), identityProvider: context.ExternalIdentity.Provider);

			return Task.FromResult(0);
		}
		/// <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;
		}