예제 #1
0
        private VKontakteAuthenticatedContext CreateAuthenticatedContext(JObject user, string accessToken,
                                                                         AuthenticationProperties properties)
        {
            var context = new VKontakteAuthenticatedContext(Context, user, accessToken)
            {
                Identity = new ClaimsIdentity(
                    Options.AuthenticationType,
                    ClaimsIdentity.DefaultNameClaimType,
                    ClaimsIdentity.DefaultRoleClaimType)
            };

            if (!string.IsNullOrEmpty(context.Id))
            {
                context.Identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, context.Id, XmlSchemaString,
                                                    Options.AuthenticationType));
            }
            if (!string.IsNullOrEmpty(context.UserName))
            {
                context.Identity.AddClaim(new Claim(ClaimsIdentity.DefaultNameClaimType, context.UserName, XmlSchemaString,
                                                    Options.AuthenticationType));
            }

            context.Properties = properties;
            return(context);
        }
        protected override async Task <AuthenticationTicket> AuthenticateCoreAsync()
        {
            AuthenticationProperties properties = null;

            try
            {
                string authorizationCode = GetParameterValueFromRequest("code");
                string state             = GetParameterValueFromRequest("state");

                properties = Options.StateDataFormat.Unprotect(state);
                if (properties == null)
                {
                    return(null);
                }

                // OAuth2 10.12 CSRF
                if (!ValidateCorrelationId(properties, logger))
                {
                    return(new AuthenticationTicket(null, properties));
                }

                JObject response = await GetAuthorizationToken(authorizationCode);

                string accessToken = (string)response["access_token"];

                JObject user = await GetUser(response, accessToken);

                VKontakteAuthenticatedContext context = CreateAuthenticatedContext(user, accessToken, properties);

                await Options.Provider.Authenticated(context);

                return(new AuthenticationTicket(context.Identity, context.Properties));
            }
            catch (Exception ex)
            {
                logger.WriteError(ex.Message);
            }
            return(new AuthenticationTicket(null, properties));
        }