Archiv der Kategorie: C#

ASP.NET C#: Windows Authentication / Single Sign On

Problem

In einer ASP.NET Seite möchte man den Windows-Anmeldebenutzer ermitteln.

Ansatz / Approach

In der Web-Anwendung muss man zunächst den Haken „Enable Anonymous Access“ entfernen und einen Haken bei „Enable Windows Authentication“ setzen.

Lösung / Solution

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for AuthenticationService
/// </summary>
public class AuthenticationService
{
   public AuthenticationService()
   {
	//
	// TODO: Add constructor logic here
	//
   }

    public string getUsername()
    {
        string windowsLogin = System.Web.HttpContext.Current.User.Identity.Name;

        if (windowsLogin == null) return "none";

        int hasDomain = windowsLogin.IndexOf(@"\");
        if (hasDomain > 0)
        {
            windowsLogin = windowsLogin.Remove(0, hasDomain + 1);
        } //end if 

        return windowsLogin;
    }
}

.NET C#: Execute command line batch file and process the output line by line (line break treatment)

Problem

A Batch File should be executed and the output processed line by line. This is especially useful when you want to generate line break treatments.

Ansatz – Approach

The usage of a stream reader helps to process the output line by line.

Solution – Lösung

Process p = new Process();

// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;

p.StartInfo.FileName = "D:\\doors_stats\\whatschanged.bat";
p.Start();

StreamReader sr = p.StandardOutput;
string output = "";
while (!sr.EndOfStream)
{
	// Do s.th. with the received line (i.e. concate)
	output += sr.ReadLine() + "\n\n<br/>";
}

.NET C# : Verzeichnis rekursiv kopieren / Copy folder with subfolders (recursive)

Problem

A directory folder with subfolders should be copied. Ein Verzeichnis mit allen Unterverzeichnissen soll rekursiv kopiert warden.

Approach

Usage of DirectoryInfo Class: getDirectories(); in combination with „foreach (DirectoryInfo subdir in dirs)“-Loop
Usage of FileInfo Class: getFiles() in combination with „foreach (FileInfo file in files)“-Loop and Object-Method „file.CopyTo(DESTINATION_FOLDER, true);“

Solution – Lösung

public void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
    // Get the subdirectories for the specified directory.
    DirectoryInfo dir = new DirectoryInfo(sourceDirName);
    DirectoryInfo[] dirs = dir.GetDirectories();

    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + sourceDirName);
    }

    // If the destination directory doesn't exist, create it.
    if (!Directory.Exists(destDirName))
    {
        Directory.CreateDirectory(destDirName);
    }

    // Get the files in the directory and copy them to the new location.
    FileInfo[] files = dir.GetFiles();
    foreach (FileInfo file in files)
    {
        string temppath = Path.Combine(destDirName, file.Name);
        file.CopyTo(temppath, true);
    }

    // If copying subdirectories, copy them and their contents to new location.
    if (copySubDirs)
    {
        foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = Path.Combine(destDirName, subdir.Name);
            DirectoryCopy(subdir.FullName, temppath, copySubDirs);
        }
    }
}