Esempio n. 1
0
        /// <summary>
        /// Loads the settings for the IdentityConfiguration from the application or web configuration file.
        /// </summary>
        /// <remarks>
        /// If there is no configuration file, or the named section does not exist, then no exception is thrown,
        /// instead the class is loaded with a set of default values.
        /// </remarks>
        protected void LoadConfiguration(IdentityConfigurationElement element)
        {
            if (element != null)
            {
                //
                // Load the claims authentication manager
                //
                if (element.ClaimsAuthenticationManager.IsConfigured)
                {
                    _claimsAuthenticationManager = GetClaimsAuthenticationManager(element);
                }

                //
                // Load the claims authorization manager.
                //
                if (element.ClaimsAuthorizationManager.IsConfigured)
                {
                    _claimsAuthorizationManager = CustomTypeElement.Resolve <ClaimsAuthorizationManager>(element.ClaimsAuthorizationManager);
                }

                //
                // Load the service level Security Token Handler configuration
                //
                _serviceHandlerConfiguration = LoadHandlerConfiguration(element);
            }

            //
            // Reads handler configuration via LoadConfiguredHandlers. Do this last.
            //
            _securityTokenHandlerCollectionManager = LoadHandlers(element);
        }
Esempio n. 2
0
        protected override void Initialize(RequestContext requestContext)
        {
            base.Initialize(requestContext);

            if (Request.Cookies["lang"] != null)
            {
                var value = Request.Cookies["lang"].Value;
                Thread.CurrentThread.CurrentCulture   = new CultureInfo(value);
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(value);
            }

            if (Request.Cookies["access_token"] != null)
            {
                var value = Request.Cookies["access_token"].Value;
                if (!string.IsNullOrEmpty(value))
                {
                    var            jwtToken     = new JwtSecurityToken(value);
                    var            claims       = jwtToken.Claims;
                    ClaimsIdentity claim        = new ClaimsIdentity(claims);
                    var            cp           = new ClaimsPrincipal(claim);
                    var            transformer  = new ClaimsAuthenticationManager();
                    var            newPrincipal = transformer.Authenticate(string.Empty, cp);
                    Thread.CurrentPrincipal = newPrincipal;
                    HttpContext.User        = newPrincipal;
                }
            }
        }
 public static IAppBuilder UseClaimsTransformation(this IAppBuilder app, ClaimsAuthenticationManager manager)
 {
     return(app.UseClaimsTransformation(new ClaimsTransformationOptions
     {
         ClaimsAuthenticationManager = manager
     }));
 }
 public OwinAuthenticationService(
     UserAccountService svc,
     IDictionary <string, object> environment,
     ClaimsAuthenticationManager transformer
     )
     : base(svc, transformer)
 {
     context = new OwinContext(environment);
 }
Esempio n. 5
0
 public OwinAuthenticationService(
     string authenticationType,
     UserAccountService svc,
     IDictionary <string, object> env,
     ClaimsAuthenticationManager transformer
     )
     : base(svc, transformer)
 {
     this.authenticationType = authenticationType;
     context = new OwinContext(env);
 }
Esempio n. 6
0
        protected void LoadConfiguration(IdentityConfigurationElement element)
        {
            if (element == null)
            {
                return;
            }

            if (element.ClaimsAuthenticationManager.Type != null)
            {
                claimsAuthenticationManager = (ClaimsAuthenticationManager)Activator.CreateInstance(
                    element.ClaimsAuthenticationManager.Type);
            }
            name = element.Name;

            serviceHandlerConfiguration = LoadHandlerConfiguration(element);
        }
Esempio n. 7
0
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                List <Claim> initialClaims = new List <Claim>();
                initialClaims.Add(new Claim(ClaimTypes.Name, model.UserName));
                ClaimsPrincipal             claimsPrincipal = new ClaimsPrincipal(new ClaimsIdentity(initialClaims, "Forms"));
                ClaimsAuthenticationManager authManager     = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.ClaimsAuthenticationManager;
                authManager.Authenticate(string.Empty, claimsPrincipal);
                return(RedirectToLocal(returnUrl));
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return(View(model));
        }
        public virtual void OnPostAuthenticateRequest(object sender, EventArgs args)
        {
            // TODO add the claimsPrincipal creation logic here
            var principal = HttpContext.Current.User as IClaimsPrincipal;

            if (principal == null)
            {
                principal = ClaimsPrincipal.CreateFromHttpContext(HttpContext.Current);

                ClaimsAuthenticationManager authenticationManager = ServiceConfiguration.ClaimsAuthenticationManager;
                if (authenticationManager != null && principal != null && principal.Identity != null)
                {
                    principal = authenticationManager.Authenticate(HttpContext.Current.Request.Url.AbsoluteUri, principal);
                }

                HttpContext.Current.User = principal;
                Thread.CurrentPrincipal  = principal;
            }
        }
        /// <summary>
        /// Event handler for Application.PostAuthenticateRequest. Runs any <see cref="ClaimsAuthenticationManager"/> if one is configured.
        /// Tries to create a <see cref="ClaimsPrincipal"/> if none is already set.
        /// </summary>
        /// <param name="sender">Sender of this event.</param>
        /// <param name="args">Event arguments.</param>
        void OnPostAuthenticateRequest(object sender, EventArgs args)
        {
            IClaimsPrincipal icp = HttpContext.Current.User as IClaimsPrincipal;

            if (icp == null)
            {
                icp = ClaimsPrincipal.CreateFromHttpContext(HttpContext.Current);

                //
                // Run the Claims Authentication Manager if one is configured
                //
                ClaimsAuthenticationManager authenticationManager = ServiceConfiguration.ClaimsAuthenticationManager;
                if (authenticationManager != null && icp != null && icp.Identity != null)
                {
                    icp = authenticationManager.Authenticate(HttpContext.Current.Request.Url.AbsoluteUri, icp);
                }

                SetPrincipal(icp);
            }
        }
Esempio n. 10
0
 public AuthenticationWrapper(UserAccountService <DAL.Entities.UserAccount> userService, ClaimsAuthenticationManager claimsAuthenticationManager) : base(userService, claimsAuthenticationManager)
 {
 }
Esempio n. 11
0
 public AuthenticationService(UserAccountService <TAccount> userService, ClaimsAuthenticationManager claimsAuthenticationManager)
 {
     this.UserAccountService          = userService;
     this.ClaimsAuthenticationManager = claimsAuthenticationManager;
 }
 public ClaimsTransformationHandler(ClaimsAuthenticationManager transformer, HttpConfiguration configuration)
 {
     _transfomer = transformer;
     InnerHandler = new HttpControllerDispatcher(configuration);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SharedKeyValidatingHandler" /> class.
 /// </summary>
 /// <param name="sharedSecretResolver">A function to resolve an account name to a shared secret.</param>
 /// <param name="maximumMessageAge">The maximum time period a message is considered valid for.</param>
 /// <param name="claimsAuthenticationManager">The claims authentication manager to use to transform claims.</param>
 public SharedKeyValidatingHandler(Func <string, byte[]> sharedSecretResolver, TimeSpan maximumMessageAge, ClaimsAuthenticationManager claimsAuthenticationManager)
     : this(sharedSecretResolver, maximumMessageAge)
 {
     this.ClaimsAuthenticationManager = claimsAuthenticationManager;
 }
 public ClaimsTransformationHandler(ClaimsAuthenticationManager transformer, HttpConfiguration configuration)
 {
     _transfomer  = transformer;
     InnerHandler = new HttpControllerDispatcher(configuration);
 }
 public MongoAuthenticationService(DefaultUserAccountServiceContainer container, ClaimsAuthenticationManager claimsAuthenticationManager, IHttpContextAccessor ctxAccessor) : base(container.Service, claimsAuthenticationManager)
 {
     _context = ctxAccessor.HttpContext;
 }
        /// <summary>
        /// Create a Claims Principal and a Federated Authentication Session for the authenticated user.
        /// </summary>
        /// <param name="lifetime">The period from the current time during which the token is valid. Default use the security token valid to time.</param>
        /// <param name="isReferenceMode">In reference mode, a simple artifact is produced during serialization and the token material is stored in the token cache that is associated with the token handler. The token cache is an instance of a class that derives from SessionSecurityTokenCache. For Web Farm scenarios, the token cache must operate across all nodes in the farm.</param>
        /// <param name="isPersistent">If the IsPersistent property is true, the cookie is written as a persistent cookie. Persistent cookies remain valid after the browser is closed until they expire.</param>
        /// <param name="claimsAuthenticationManager">Possible to add a custom ClaimsAuthenticationManager for handling claims transformation.</param>
        public static ClaimsPrincipal CreateSession(this Saml2AuthnResponse saml2AuthnResponse, TimeSpan?lifetime = null, bool isReferenceMode = false, bool isPersistent = false, ClaimsAuthenticationManager claimsAuthenticationManager = null)
        {
            if (Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                throw new InvalidOperationException("There already exist an Authenticated user.");
            }

            if (saml2AuthnResponse.Status != Saml2StatusCodes.Success)
            {
                throw new InvalidOperationException($"The SAML2 Response Status is not Success, the Response Status is: {saml2AuthnResponse.Status}.");
            }

            var principal = new ClaimsPrincipal(saml2AuthnResponse.ClaimsIdentity);

            if (principal.Identity == null || !principal.Identity.IsAuthenticated)
            {
                throw new InvalidOperationException("No Claims Identity created from SAML2 Response.");
            }

            var transformedPrincipal = claimsAuthenticationManager != null?claimsAuthenticationManager.Authenticate(null, principal) : principal;

            var sessionSecurityToken = lifetime.HasValue ?
                                       new SessionSecurityToken(transformedPrincipal, lifetime.Value) :
                                       new SessionSecurityToken(transformedPrincipal, null, saml2AuthnResponse.Saml2SecurityToken.ValidFrom, saml2AuthnResponse.Saml2SecurityToken.ValidTo);

            sessionSecurityToken.IsReferenceMode = isReferenceMode;
            sessionSecurityToken.IsPersistent    = isPersistent;
            FederatedAuthentication.SessionAuthenticationModule.AuthenticateSessionSecurityToken(sessionSecurityToken, true);
            return(transformedPrincipal);
        }
 public AspNetAuthenticationService(UserAccountService <ApplicationUser> userService, ClaimsAuthenticationManager claimsAuthenticationManager, HttpContext context)
     : this(MembershipRebootApplicationConstants.AuthenticationType, userService, claimsAuthenticationManager, context)
 {
 }
 public ClaimsTransformationHandler(ClaimsAuthenticationManager transformer)
 {
     _transfomer = transformer;
 }
 public ClaimsTransformationHandler()
 {
     _transfomer = FederatedAuthentication.ServiceConfiguration.ClaimsAuthenticationManager;
 }
 public AspNetAuthenticationService(string authenticationScheme, UserAccountService <ApplicationUser> userService, ClaimsAuthenticationManager claimsAuthenticationManager, HttpContext context) : base(userService, claimsAuthenticationManager)
 {
     _authenticationScheme = authenticationScheme;
     _context = context;
 }
Esempio n. 21
0
 protected AuthenticationService(IUserAccountService userService,
                                 ClaimsAuthenticationManager claimsAuthenticationManager)
 {
     UserAccountService          = userService;
     ClaimsAuthenticationManager = claimsAuthenticationManager;
 }
 public AuthenticationWrapper(BrockAllen.MembershipReboot.UserAccountService <UserAccount> userService,
                              ClaimsAuthenticationManager claimsAuthenticationManager) : base(userService, claimsAuthenticationManager)
 {
 }