Skip to content

Instantly share code, notes, and snippets.

@fdervishi90
Last active February 3, 2019 15:19
Show Gist options
  • Save fdervishi90/d7dd7a2224c62b351f7f915feae5d9ed to your computer and use it in GitHub Desktop.
Save fdervishi90/d7dd7a2224c62b351f7f915feae5d9ed to your computer and use it in GitHub Desktop.
#asp #mvc
// Send mail c#
public static class Mail
{
public const string Host = "smtp.gmail.com";
//public const string Host = "exchange.tgroupcc.com";
public static MailAddress FromAddress { get; set; } = new MailAddress("greeti.albania@gmail.com", "GREETi");
public static string FromPassword { get; set; } = "Greeti2019";
public static string Subject { get; set; } = "GREETi: You have visitors.";
public static async void SendMail(MailAddress toAddress, string visitor)
{
SmtpClient smtp = new SmtpClient
{
Host = Host,
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(FromAddress.Address, FromPassword)
};
using (MailMessage message = new MailMessage(FromAddress, toAddress)
{
Subject = Subject,
Body = $"Hi {toAddress.DisplayName},<br /><br />{visitor} has come to visit you.<br /><br />Best Regards, <br /><br />GREETi",
IsBodyHtml = true
})
{
try
{
await smtp.SendMailAsync(message);
//smtp.Send(message);
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
}
}
}
}
// Loop through all groups for users
public IEnumerable<Employee> GetEmployees()
{
PrincipalContext context = new PrincipalContext(ContextType.Domain, "targetpower.be", "OU=Staff,OU=Groups,OU=Main,dc=targetpower,dc=be");
GroupPrincipal groupPrincipal = new GroupPrincipal(context);
PrincipalSearcher searcher = new PrincipalSearcher(groupPrincipal);
List<Employee> employees = new List<Employee>();
foreach (Principal principal in searcher.FindAll())
{
GroupPrincipal group = GroupPrincipal.FindByIdentity(context, principal.Name);
if (@group != null)
foreach (Principal userPrincipal in @group.Members)
{
employees.Add(new Employee
{
Name = userPrincipal.Name,
SamAccountName = userPrincipal.SamAccountName,
Displayname = userPrincipal.DisplayName,
Description = userPrincipal.Description,
DistinguishedName = userPrincipal.DistinguishedName,
StructuralObjectClass = userPrincipal.StructuralObjectClass,
UserPrincipalName = userPrincipal.UserPrincipalName
});
}
}
return employees.GroupBy(e => e.Name)
.Select(e => e.First());
}
// Getting users from a starting OU in Active Directory
public IEnumerable<Employee> GetEmployees()
{
PrincipalContext context = new PrincipalContext(ContextType.Domain, "targetpower.be","OU=Users,OU=Main,dc=targetpower,dc=be");
UserPrincipal userPrincipal = new UserPrincipal(context);
PrincipalSearcher searcher = new PrincipalSearcher(userPrincipal);
List<Employee> employees = new List<Employee>();
foreach (Principal principal in searcher.FindAll())
{
employees.Add(new Employee { Name = principal.Name,
SamAccountName = principal.SamAccountName,
Displayname = principal.DisplayName,
Description = principal.Description,
DistinguishedName = principal.DistinguishedName,
StructuralObjectClass = principal.StructuralObjectClass,
UserPrincipalName = principal.UserPrincipalName
});
}
return employees;
//return searcher.FindAll();
}
// Get user details from active directory
public List<Employee> GetEmployees()
{
List<Employee> employees = new List<Employee>();
var context = new PrincipalContext(ContextType.Domain, "targetpower.be");
var searcher = new PrincipalSearcher(new UserPrincipal(context));
List<UserPrincipal> users = searcher.FindAll().Select(u => (UserPrincipal)u).Where(u => u.Enabled == true).Take(20).ToList();
foreach (var u in users)
{
DirectoryEntry d = (DirectoryEntry)u.GetUnderlyingObject();
if (d.Properties["GivenName"].Value != null && d.Properties["sn"].Value != null)
{
employees.Add(new Employee { GivenName = d.Properties["GivenName"]?.Value?.ToString(), LastName = d.Properties["sn"]?.Value?.ToString() });
}
}
return employees;
}
// Many to many config between User and Links
modelBuilder.Entity<User>()
.HasMany(u => u.Links)
.WithMany(l => l.Users)
.Map(m =>
{
m.ToTable("tblUserLinks");
m.MapLeftKey("UserID");
m.MapRightKey("LinkID");
});
// Allow direct anonymous access to mvc project with windows authentication
<location path="api/Companies/GetCompanies">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment