Exemplo n.º 1
0
        public void ConfigureAuth(IAppBuilder app)
        {
            DataProtectionProvider = app.GetDataProtectionProvider();
            var UseActiveDirectory = System.Configuration.ConfigurationManager.AppSettings["UseActiveDirectory"];     //CommonFunction.Instance.UseActiveDirectory();

            if (!Convert.ToBoolean(UseActiveDirectory))
            {
                // Enable the application to use a cookie to store information for the signed in user

                //set session time out value from appsetting
                ApplicationContext db = new ApplicationContext(new SystemUser());
                var    appSettings    = db.AppSettings;
                string timeout        = appSettings.Where(p => p.Key == "ApplicationSessionTimeOut").FirstOrDefault().Value;
                string AppUrl         = appSettings.Where(p => p.Key == "AppURL").FirstOrDefault().Value;
                Int64  TimeOutValue   = 525600;
                if (!string.IsNullOrEmpty(timeout))
                {
                    if (Int64.TryParse(timeout, out TimeOutValue))
                    {
                        if (TimeOutValue == 0)
                        {
                            TimeOutValue = Convert.ToInt64(525600);
                        }
                    }
                    else
                    {
                        TimeOutValue = 525600;
                    }
                }
                //
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    CookieName         = AppUrl,
                    ExpireTimeSpan     = TimeSpan.FromMinutes(TimeOutValue),
                    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                    LoginPath          = new PathString("/Account/Login")
                });
                // Use a cookie to temporarily store information about a user logging in with a third party login provider
                app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
                ThirdPartyLoginRepository thirdPartyLoginRepository = new ThirdPartyLoginRepository();
                ThirdPartyLogin           thirdPartyLogin           = thirdPartyLoginRepository.GetThirdPartyLogin();
                //Google
                // Uncomment the following lines to enable logging in with third party login providers
                GooglePlusAuthenticationOptions google = new GooglePlusAuthenticationOptions();
                google.ClientId     = thirdPartyLogin.GooglePlusId;
                google.ClientSecret = thirdPartyLogin.GooglePlusSecretKey;
                //commmented to use google authentication from GooleAuthApp
                app.UseGooglePlusAuthentication(google);
                //Yahoo
                OAuthWebSecurity.RegisterYahooClient();
                app.UseOpenIDAuthentication("/open.login.yahooapis.com/openid/op/auth", "Yahoo");
                //Facebook
                var x = new FacebookAuthenticationOptions();
                x.Scope.Add("email");
                x.Scope.Add("friends_about_me");
                x.Scope.Add("friends_photos");
                x.AppId     = thirdPartyLogin.FacebookId;
                x.AppSecret = thirdPartyLogin.FacebookSecretKey;
                x.Provider  = new FacebookAuthenticationProvider()
                {
                    OnAuthenticated = async context =>
                    {
                        //Get the access token from FB and store it in the database and
                        //use FacebookC# SDK to get more information about the user
                        context.Identity.AddClaim(
                            new System.Security.Claims.Claim("FacebookAccessToken", context.AccessToken));
                    }
                };
                x.SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie;
                app.UseFacebookAuthentication(x);
            }
        }
Exemplo n.º 2
0
        public void ConfigureOAuth(IAppBuilder app)
        {
            app.CreatePerOwinContext(DBContext.Create);
            app.CreatePerOwinContext <ApplicationUserManager>(ApplicationUserManager.Create);
            app.CreatePerOwinContext <ApplicationRoleManager>(ApplicationRoleManager.Create);

            //app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

            //use a cookie to temporarily store information about a user logging in with a third party login provider
            app.UseExternalSignInCookie(Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie);
            OAuthBearerOptions = new OAuthBearerAuthenticationOptions();

            OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
            {
                AllowInsecureHttp         = true,
                TokenEndpointPath         = new PathString("/token"),
                AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
                Provider             = new SimpleAuthorizationServerProvider(),
                RefreshTokenProvider = new SimpleRefreshTokenProvider()
            };

            // Token Generation
            app.UseOAuthAuthorizationServer(OAuthServerOptions);
            app.UseOAuthBearerAuthentication(OAuthBearerOptions);

            //Configure Google External Login
            googleAuthOptions = new GooglePlusAuthenticationOptions()
            {
                ClientId     = ConfigurationManager.AppSettings["GooglePlusClientId"],
                ClientSecret = ConfigurationManager.AppSettings["GooglePlusClientSecret"],
                Provider     = new GoogleAuthProvider(),
                CallbackPath = new PathString("/signin-googleplus")
            };
            // TODO uncomment to use G+ auth
            app.UseGooglePlusAuthentication(googleAuthOptions);

            string stripeClientId     = ConfigurationManager.AppSettings["StripeClientId"];
            string stripeClientSecret = ConfigurationManager.AppSettings["StripeClientSecret"];

            stripeAuthOptions = new StripeAuthenticationOptions()
            {
                ClientId     = stripeClientId,
                ClientSecret = stripeClientSecret,
                Provider     = new StripeAuthProvider()
                {
                    OnAuthenticated = async context =>
                    {
                        context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken));
                        context.Identity.AddClaim(new Claim("urn:stripe:refreshToken", context.RefreshToken));
                    }
                },
                SignInAsAuthenticationType = Microsoft.AspNet.Identity.DefaultAuthenticationTypes.ExternalCookie
            };
            app.UseStripeAuthentication(stripeAuthOptions);



            string linkedinApiKey = ConfigurationManager.AppSettings["LinkedInApiKey"];
            string linkedinSecret = ConfigurationManager.AppSettings["LinkedInSecret"];

            linkedinAuthOptions = new LinkedInAuthenticationOptions()
            {
                ClientId     = linkedinApiKey,
                ClientSecret = linkedinSecret,
                Provider     = new LinkedInAuthProvider
                {
                    OnAuthenticated = async context =>
                    {
                        context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken));
                    }
                },
                SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie
            };
            app.UseLinkedInAuthentication(linkedinAuthOptions);


            string githubClientId     = ConfigurationManager.AppSettings["GithubClientId"];
            string githubClientSecret = ConfigurationManager.AppSettings["GithubClientSecret"];

            githubAuthOptions = new GitHubAuthenticationOptions()
            {
                ClientId     = githubClientId,
                ClientSecret = githubClientSecret,
                Provider     = new GitHubAuthProvider
                {
                    OnAuthenticated = async context =>
                    {
                        context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken));
                    }
                },
                SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie
            };

            app.UseGitHubAuthentication(githubAuthOptions);

            string twitterConsumerKey    = ConfigurationManager.AppSettings["TwitterConsumerKey"];
            string twitterConsumerSecret = ConfigurationManager.AppSettings["TwitterConsumerSecret"];

            twitterAuthOptions = new TwitterAuthenticationOptions()
            {
                ConsumerKey    = githubClientId,
                ConsumerSecret = githubClientSecret,
                Provider       = new TwitterAuthProvider
                {
                    OnAuthenticated = async context =>
                    {
                        context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken));
                    }
                },
                SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie
            };

            app.UseTwitterAuthentication(twitterAuthOptions);


            string stackexchangeClientId     = ConfigurationManager.AppSettings["StackExchangeClientId"];
            string stackexchangeClientSecret = ConfigurationManager.AppSettings["StackExchangeClientSecret"];
            string stackexchangeKey          = ConfigurationManager.AppSettings["StackExchangeKey"];

        #if DEBUG
            stackexchangeClientId     = ConfigurationManager.AppSettings["StackExchangeClientIdDEBUG"];
            stackexchangeClientSecret = ConfigurationManager.AppSettings["StackExchangeClientSecretDEBUG"];
            stackexchangeKey          = ConfigurationManager.AppSettings["StackExchangeKeyDEBUG"];
        #endif

            stackexchangeAuthOptions = new StackExchangeAuthenticationOptions()
            {
                ClientId     = stackexchangeClientId,
                ClientSecret = stackexchangeClientSecret,
                Key          = stackexchangeKey,
                Provider     = new StackExchangeAuthProvider
                {
                    OnAuthenticated = async context =>
                    {
                        context.Identity.AddClaim(new Claim("ExternalAccessToken", context.AccessToken));
                    }
                },
                SignInAsAuthenticationType = DefaultAuthenticationTypes.ExternalCookie
            };

            app.UseStackExchangeAuthentication(stackexchangeAuthOptions);


            //Configure Facebook External Login
            //facebookAuthOptions = new FacebookAuthenticationOptions()
            //{
            //    AppId = "xxx",
            //    AppSecret = "xxx",
            //    Provider = new FacebookAuthProvider()
            //};
            //app.UseFacebookAuthentication(facebookAuthOptions);
        }