Giter Club home page Giter Club logo

owin-mixedauth's Introduction

OWIN Mixed Authentication

OWIN middleware implementation mixing Windows and Forms Authentication.

mixed-auth

Install with NuGet

PM> Install-Package OWIN-MixedAuth

Running the samples

Before running the samples, make sure to unlock windowsAuthentication section:

IIS

  1. Open IIS Manager, select the server node, then Feature Delegation.
  2. Set Authentication - Windows to Read/Write

unlock-section

IIS Express

  1. Open applicationhost.config located at:
  • Pre VS2015: $:\Users\{username}\Documents\IISExpress\config
  • VS2015: $(SolutionDir)\.vs\config
  1. Search for windowsAuthentication section and update overrideModeDefault value to Allow.
 <section name="windowsAuthentication" overrideModeDefault="Allow" />

Usage

  1. Add reference to MohammadYounes.Owin.Security.MixedAuth.dll

  2. Register MixedAuth in Global.asax

//add using statement
using MohammadYounes.Owin.Security.MixedAuth;

public class MyWebApplication : HttpApplication
{
   //ctor
   public MyWebApplication()
   {
     //register MixedAuth
     this.RegisterMixedAuth();
   }
   .
   .
   .
}
  1. Use MixedAuth in Startup.Auth.cs
//Enable Mixed Authentication
//As we are using LogonUserIdentity, its required to run in PipelineStage.PostAuthenticate
//Register this after any middleware that uses stage marker PipelineStage.Authenticate

app.UseMixedAuth(cookieOptions);

Important! MixedAuth is required to run in PipelineStage.PostAuthenticate, make sure the use statement is after any other middleware that uses PipelineStage.Authenticate. See OWIN Middleware in the IIS integrated pipeline.

  1. Enable Windows authentication in Web.config
<!-- Enable Mixed Auth -->
<location path="MixedAuth">
  <system.webServer>
    <security>
      <authentication>
        <windowsAuthentication enabled="true" />
      </authentication>
    </security>
  </system.webServer>
</location>

Important! Enabling windows authentication for a sub path requires windowsAuthentication section to be unlocked at a parent level.


Importing Custom Claims

Adding custom claims in OWIN-MixedAuth is pretty straightforward, simply use MixedAuthProvider and place your own logic for fetching those custom claims.

The following example shows how to import user Email, Surname and GiveName from Active Directory:

// Enable mixed auth
 app.UseMixedAuth(new MixedAuthOptions()
 {
   Provider = new MixedAuthProvider()
   {
     OnImportClaims = identity =>
     {
       List<Claim> claims = new List<Claim>();
       using (var principalContext = new PrincipalContext(ContextType.Domain)) //or ContextType.Machine
       {
         using (UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(principalContext, identity.Name))
         {
           if (userPrincipal != null)
           {
             claims.Add(new Claim(ClaimTypes.Email, userPrincipal.EmailAddress ?? string.Empty));
             claims.Add(new Claim(ClaimTypes.Surname, userPrincipal.Surname ?? string.Empty));
             claims.Add(new Claim(ClaimTypes.GivenName, userPrincipal.GivenName ?? string.Empty));
           }
         }
       }
       return claims;
     }
   }
 }, cookieOptions);

Please share any issues you may have.

owin-mixedauth's People

Contributors

mohammadyounes avatar

Watchers

James Cloos avatar Jennifer Bartolome avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.