/// <summary> /// This methods gets called for local authentication (whenever the user uses the username and password dialog). /// </summary> /// <param name="username">The username.</param> /// <param name="password">The password.</param> /// <param name="message">The signin message.</param> /// <returns> /// The authentication result /// </returns> public virtual Task<AuthenticateResult> AuthenticateLocalAsync(string username, string password, SignInMessage message) { var query = from u in _users where u.Username == username && u.Password == password select u; var user = query.SingleOrDefault(); if (user != null) { var p = IdentityServerPrincipal.Create(user.Subject, GetDisplayName(user), Constants.AuthenticationMethods.Password, Constants.BuiltInIdentityProvider); var result = new AuthenticateResult(p); return Task.FromResult(result); } return Task.FromResult<AuthenticateResult>(null); }
/// <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); }