public void ProcessRequest(HttpContext context)
        {
            var token   = client.ExchangeAuthorizationCodePerAccessToken(context.Request.QueryString["code"], context.Request.Url.ToString());
            var profile = client.GetUserInfo(token);

            var user = new List <KeyValuePair <string, object> >
            {
                new KeyValuePair <string, object>("name", profile.Name),
                new KeyValuePair <string, object>("email", profile.Email),
                new KeyValuePair <string, object>("family_name", profile.FamilyName),
                new KeyValuePair <string, object>("gender", profile.Gender),
                new KeyValuePair <string, object>("given_name", profile.GivenName),
                new KeyValuePair <string, object>("nickname", profile.Nickname),
                new KeyValuePair <string, object>("picture", profile.Picture),
                new KeyValuePair <string, object>("user_id", profile.UserId),
                new KeyValuePair <string, object>("id_token", token.IdToken),
                new KeyValuePair <string, object>("access_token", token.AccessToken),
                new KeyValuePair <string, object>("connection", profile.Identities.First().Connection),
                new KeyValuePair <string, object>("provider", profile.Identities.First().Provider)
            };

            // NOTE: Uncomment the following code in order to include claims from associated identities
            //profile.Identities.ToList().ForEach(i =>
            //{
            //    user.Add(new KeyValuePair<string, object>(i.Connection + ".access_token", i.AccessToken));
            //    user.Add(new KeyValuePair<string, object>(i.Connection + ".provider", i.Provider));
            //    user.Add(new KeyValuePair<string, object>(i.Connection + ".user_id", i.UserId));
            //});

            // NOTE: uncomment this if you send roles
            // user.Add(new KeyValuePair<string, object>(ClaimTypes.Role, profile.ExtraProperties["roles"]));

            // NOTE: this will set a cookie with all the user claims that will be converted
            //       to a ClaimsPrincipal for each request using the SessionAuthenticationModule HttpModule.
            //       You can choose your own mechanism to keep the user authenticated (FormsAuthentication, Session, etc.)
            FederatedAuthentication.SessionAuthenticationModule.CreateSessionCookie(user);

            if (context.Request.QueryString["state"] != null && context.Request.QueryString["state"].StartsWith("ru="))
            {
                var state = HttpUtility.ParseQueryString(context.Request.QueryString["state"]);
                context.Response.Redirect(state["ru"], true);
            }

            context.Response.Redirect("/");
        }
示例#2
0
        public static IResponseFormatter AuthenticateThisSession(this NancyModule module)
        {
            var code = (string)module.Request.Query["code"];

            var token = Auth0Client.ExchangeAuthorizationCodePerAccessToken(code,
                                                                            ConfigurationManager.AppSettings["auth0:CallbackUrl"]);

            var userInfo = Auth0Client.GetUserInfo(token);

            var user = new Auth0User
            {
                AccessToken = token.AccessToken,
                UserToken   = token.IdToken,
                UserId      = userInfo.UserId,
                Name        = userInfo.Name,
                Nickname    = userInfo.Nickname,
                GravatarUrl = userInfo.Picture,
                Email       = userInfo.Email
            };

            Auth0Authentication.CreateAuthenticationSessionFor(user, module.Context.Request.Session);

            return(module.Response);
        }