using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin;
using OwinSample.BasicAuthMiddleware;
using System.Security.Claims;
using Microsoft.Owin.Security;

namespace OwinSample
{
    public class SecurePassiveController: ApiController
    {

        public async Task<string> GetUserName()
        {
            var authenticationManager = Request.GetOwinContext().Authentication;
            var authResult = await authenticationManager.AuthenticateAsync("Basic");

            // Falls die Authentifizierung fehlgeschagen ist, ist authResult null,
            // selbst wenn AuthenticateAsync ein AuthenticationTicket, dessen 
            // Identity null ist, retourniert hat
            if (authResult != null)
            {
                var prop = authResult.Properties;
                Debug.WriteLine("WasStrongPassword: " + prop.Dictionary["WasStrongPassword"]);
            }

            var p = new AuthenticationProperties();
            p.Dictionary["info"] = "Some additional information";

            //authenticationManager.Challenge(p, "Basic");
            authenticationManager.SignIn(p, new ClaimsIdentity("Basic"));
            authenticationManager.SignOut("Basic");
            

            // if (authResult == null || authResult.Identity == null) throw new HttpResponseException(HttpStatusCode.Unauthorized);




            return authResult.Identity.Name;
        }
    }
}
