Ejemplo n.º 1
0
        public JObject GenerateLocalAccessTokenResponse(SocialRegisterModel socialRegisterModel)
        {
            //ToSocialregisterModel
            try
            {
                if (socialRegisterModel != null)
                {
                    var tokenExpiration = TimeSpan.FromSeconds(2);
                    var props           = new AuthenticationProperties()
                    {
                        IssuedUtc  = DateTime.UtcNow,
                        ExpiresUtc = DateTime.UtcNow.Add(tokenExpiration),
                    };
                    var            user     = IService.ToSocialregisterModel(socialRegisterModel);
                    ClaimsIdentity identity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
                    identity.AddClaim(new Claim(ClaimTypes.Role, "SuperAdmin"));
                    identity.AddClaim(new Claim(ClaimTypes.Name, socialRegisterModel.UserName));
                    identity.AddClaim(new Claim("UserId", user.Id.ToString()));
                    identity.AddClaim(new Claim("DisplayName", user.DisplayName));
                    identity.AddClaim(new Claim("isAdmin", user.IsAdmin.ToString()));
                    identity.AddClaim(new Claim("LastLoginDate", user.LastLoginTime.ToString()));

                    AuthenticationProperties properties = AuthorizationServerProvider.CreateProperties(
                        socialRegisterModel.UserName, user.Id.ToString(), user.DisplayName, user.IsNGO.ToString(), user.CanEndorse.ToString(),
                        user.IsAdmin.ToString(), user.LastLoginTime.ToString());

                    //        //AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
                    AuthenticationTicket ticket = new AuthenticationTicket(identity, properties);

                    // var ticket = new AuthenticationTicket(identity, props);

                    var accessToken = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket);

                    JObject tokenResponse = new JObject(
                        new JProperty("userName", socialRegisterModel.UserName),
                        new JProperty("UserId", user.Id.ToString()),
                        new JProperty("DisplayName", user.DisplayName),
                        new JProperty("IsNGO", user.IsNGO.ToString()),
                        new JProperty("canEndorse", user.CanEndorse.ToString()),
                        new JProperty("access_token", accessToken),
                        new JProperty("token_type", "bearer"),
                        new JProperty("expires_in", tokenExpiration.TotalSeconds.ToString()),
                        new JProperty(".issued", ticket.Properties.IssuedUtc.ToString()),
                        new JProperty(".expires", ticket.Properties.ExpiresUtc.ToString()),
                        new JProperty(".isAdmin", user.IsAdmin.ToString()),
                        new JProperty(".LastLoginDate", user.LastLoginTime.ToString())
                        );

                    return(tokenResponse);
                }
                else
                {
                    JObject tokenResponse = new JObject(new JProperty("response", "BadRequest"));
                    return(tokenResponse);
                }
            }
            catch (Exception ex)
            { throw ex; }
        }
Ejemplo n.º 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 = AuthorizationServerProvider.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());
        }