public void Arrange()
        {
            _customOauthProvider = new CustomOAuthProvider(new PortalJwtTokenConfiguration {
                AudienceId = "audience_id"
            });

            var owinContext = new OwinContext();

            owinContext.Set("otc:username", "test");
            _context = new OAuthGrantResourceOwnerCredentialsContext(owinContext, new OAuthAuthorizationServerOptions(), "", "test", "test", new List <string>());
        }
Beispiel #2
0
        public async Task <IHttpActionResult> GetExternalLogin(string provider, string error = null)
        {
            if (error != null)
            {
                return(Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error)));
            }

            if (!User.Identity.IsAuthenticated)
            {
                return(new ChallengeResult(provider, this));
            }

            ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

            if (externalLogin == null)
            {
                return(InternalServerError());
            }

            if (externalLogin.LoginProvider != provider)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
                return(new ChallengeResult(provider, this));
            }

            ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider,
                                                                                 externalLogin.ProviderKey));

            bool hasRegistered = user != null;

            if (hasRegistered)
            {
                Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

                ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager, OAuthDefaults.AuthenticationType);

                ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager, CookieAuthenticationDefaults.AuthenticationType);

                AuthenticationProperties properties = CustomOAuthProvider.CreateProperties(user.UserName, "");
                Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
            }
            else
            {
                IEnumerable <Claim> claims   = externalLogin.GetClaims();
                ClaimsIdentity      identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
                Authentication.SignIn(identity);
            }

            return(Ok());
        }
        public void ConfigureOAuth(IAppBuilder app)
        {
            var issuer = ConfigurationManager.AppSettings["issuer"];

            app.CreatePerOwinContext(() => new CustomUserManager(new CustomUserStore(new UsersAuthorizationStore())));

            app.UseCookieAuthentication(new CookieAuthenticationOptions());
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

            app.UseOAuthAuthorizationServer(new OAuthAuthorizationServerOptions
            {
                TokenEndpointPath         = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(30),
                Provider          = new CustomOAuthProvider(),
                AccessTokenFormat = new CustomJwtFormat(issuer),
#if DEBUG
                AllowInsecureHttp = true
#else
                AllowInsecureHttp = false
#endif
            });
        public IActionResult Post(AuthenticateRequest model)
        {
            try
            {
                var user = _userService.Authenticate(model);

                if (user == null)
                {
                    _logger.LogInformation("Username or password is incorrect");
                    return(BadRequest(new { message = "Username or password is incorrect" }));
                }

                var token = new CustomOAuthProvider(_appSettings).GenerateJwtToken(user);

                return(Ok(new AuthenticateResponse(user, token)));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error occured in Authenticate");
                return(BadRequest("Error occured."));
            }
        }
 public void Arrange()
 {
     _customOauthProvider = new CustomOAuthProvider(new PortalJwtTokenConfiguration {
         AudienceId = "audience"
     });
 }