Table of contents

  1. ASP.NET 5 / MVC 6 On-Premises Active Directory
  2. ASP 5, MVC 6 sending email
  3. Asp.Net 5 MVC 6 detect mobile browser

ASP.NET 5 / MVC 6 On-Premises Active Directory

ASP.NET 5 (also known as ASP.NET Core) and MVC 6 provide several options for integrating with on-premises Active Directory. Here are some possible approaches:

  1. Use Windows Authentication: Windows Authentication is a feature of IIS that allows ASP.NET applications to authenticate users against a domain controller in an Active Directory environment. When you enable Windows Authentication in your ASP.NET application, IIS automatically authenticates the user based on their Windows identity. This approach can work well if you want to use the existing authentication infrastructure in your organization.

  2. Use LDAP: Another approach is to use the Lightweight Directory Access Protocol (LDAP) to authenticate users against Active Directory. With this approach, you can use the System.DirectoryServices namespace in .NET to connect to the LDAP server and authenticate users. You can also use the Novell.Directory.Ldap library to authenticate against an LDAP server.

  3. Use OpenID Connect: OpenID Connect is an authentication protocol that allows users to authenticate against a variety of identity providers, including Active Directory. With this approach, you can use the Microsoft.AspNetCore.Authentication.OpenIdConnect library to authenticate users against Active Directory.

  4. Use a third-party identity provider: If you don't want to authenticate users against Active Directory directly, you can use a third-party identity provider that supports Active Directory integration. Examples of such providers include Okta, Auth0, and Azure Active Directory.

When choosing an approach, it's important to consider factors such as security, ease of implementation, and compatibility with your existing infrastructure. You may also want to consult with your IT department or security team to ensure that your implementation meets any relevant security requirements.


ASP 5, MVC 6 sending email

In ASP.NET 5 and MVC 6, you can use the SmtpClient class from the System.Net.Mail namespace to send email messages from your application. Here's an example:

using System.Net;
using System.Net.Mail;

public async Task SendEmailAsync(string email, string subject, string message)
{
    var smtpClient = new SmtpClient("smtp.gmail.com", 587)
    {
        UseDefaultCredentials = false,
        Credentials = new NetworkCredential("[email protected]", "your-password"),
        EnableSsl = true,
    };

    var mailMessage = new MailMessage
    {
        From = new MailAddress("[email protected]"),
        Subject = subject,
        Body = message,
        IsBodyHtml = true,
    };
    mailMessage.To.Add(email);

    await smtpClient.SendMailAsync(mailMessage);
}

In this example, we're creating a new instance of the SmtpClient class and setting its properties to specify the SMTP server details, credentials, and SSL settings. We're then creating a new MailMessage instance and setting its From, To, Subject, Body, and IsBodyHtml properties. Finally, we're calling the SendMailAsync method of the SmtpClient class to send the message asynchronously.

Note that in order to use the SmtpClient class to send email, you'll need to provide valid SMTP server details, credentials, and SSL settings. In this example, we're using Gmail's SMTP server as an example, but you'll need to replace the server details with the SMTP server details provided by your email provider.

Also, note that you'll need to enable access to less secure apps in your Gmail account if you're using Gmail as the email provider. You can do this by going to your Google Account > Security > Less secure app access and turning on the "Allow less secure apps" option.


Asp.Net 5 MVC 6 detect mobile browser

In ASP.NET 5 MVC 6, you can detect a mobile browser using the Microsoft.AspNetCore.Http.Features.HttpRequestFeature class to access the HTTP user agent string and determine whether it matches a known mobile browser.

Here's an example:

using Microsoft.AspNetCore.Http.Features;

public class ExampleController : Controller
{
    public IActionResult Index()
    {
        var userAgent = Request.HttpContext.Features.Get<IHttpRequestFeature>().Headers["User-Agent"].ToString();

        if (IsMobileBrowser(userAgent))
        {
            return RedirectToAction("MobileIndex");
        }
        else
        {
            return View();
        }
    }

    public IActionResult MobileIndex()
    {
        return View();
    }

    private bool IsMobileBrowser(string userAgent)
    {
        // List of known mobile user agent strings
        var mobileUserAgents = new[] { "iPhone", "Android", "Windows Phone" };

        foreach (var mobileUserAgent in mobileUserAgents)
        {
            if (userAgent.Contains(mobileUserAgent))
            {
                return true;
            }
        }

        return false;
    }
}

In this example, we create an Index action that detects whether the browser is mobile by calling the IsMobileBrowser method and redirecting to a MobileIndex action if the browser is mobile. The IsMobileBrowser method checks whether the user agent string contains any of the known mobile user agent strings, and returns true if it does.

Note that this approach is not foolproof, as it relies on the HTTP user agent string, which can be spoofed or altered by the user. Additionally, there are many different mobile devices and browsers, so it's difficult to create a comprehensive list of known mobile user agent strings.


More Python Questions

More C# Questions