Пример #1
0
        private MembershipUser Authorize(FacebookPrincipalSerializableModel model, out string[] roles)
        {
            MembershipUser mUser = null;

            roles = null;

            lock (locker) {
                //if (Membership.ValidateUser(model.UniqueLink,model.FacebookId)) {

                var col = Membership.FindUsersByName(model.UniqueLink);
                if (col != null && col.Count > 0)
                {
                    foreach (MembershipUser user in col)
                    {
                        if (user.GetPassword() == model.FacebookId)
                        {
                            mUser = user;
                        }
                    }
                }
                if (mUser != null)
                {
                    roles = Roles.GetRolesForUser(mUser.UserName);
                }
                if (mUser == null)
                {
                    //New user
                    mUser = Membership.CreateUser(model.UniqueLink, model.FacebookId, model.EMail);
                    roles = new string[] { "ForumUser" }; //Free read access to the forum

                    //Special NFORUM attributes
                    var membersfound = Member.GetMemberByName(model.UniqueLink, true);
                    if (membersfound != null && membersfound.Length > 0)
                    {
                        var m = membersfound[0];
                        if (m != null)
                        {
                            m.getProperty("forumUserTwitterUrl").Value           = String.Empty;
                            m.getProperty("forumUserPosts").Value                = 0;
                            m.getProperty("forumUserKarma").Value                = 0;
                            m.getProperty("forumUserAllowPrivateMessages").Value = 1;
                            m.getProperty("forumUserLastPrivateMessage").Value   = DateTime.Now;
                            m.getProperty("forumUserIsAdmin").Value              = 0;
                            m.getProperty("forumUserIsBanned").Value             = 0;
                            m.getProperty("forumUserIsAuthorised").Value         = 1;

                            var mg = MemberGroup.GetByName("ForumUser");
                            m.AddGroup(mg.Id);
                        }
                    }
                }
            }

            return(mUser);
        }
        private MembershipUser Authorize(FacebookPrincipalSerializableModel model, out string[] roles)
        {
            MembershipUser mUser = null;
            roles = null;

            lock(locker) {

                //if (Membership.ValidateUser(model.UniqueLink,model.FacebookId)) {

                var col = Membership.FindUsersByName(model.UniqueLink);
                if (col != null && col.Count > 0)
                    foreach (MembershipUser user in col)
                    {
                        if (user.GetPassword() == model.FacebookId)
                            mUser = user;
                    }
                if (mUser != null)
                {
                    roles = Roles.GetRolesForUser(mUser.UserName);
                }
                if (mUser == null)
                {
                    //New user
                    mUser = Membership.CreateUser(model.UniqueLink, model.FacebookId, model.EMail);
                    roles = new string[] { "ForumUser" }; //Free read access to the forum

                    //Special NFORUM attributes
                    var membersfound = Member.GetMemberByName(model.UniqueLink, true);
                    if (membersfound != null && membersfound.Length > 0)
                    {
                        var m = membersfound[0];
                        if (m != null)
                        {
                            m.getProperty("forumUserTwitterUrl").Value = String.Empty;
                            m.getProperty("forumUserPosts").Value = 0;
                            m.getProperty("forumUserKarma").Value = 0;
                            m.getProperty("forumUserAllowPrivateMessages").Value = 1;
                            m.getProperty("forumUserLastPrivateMessage").Value = DateTime.Now;
                            m.getProperty("forumUserIsAdmin").Value = 0;
                            m.getProperty("forumUserIsBanned").Value = 0;
                            m.getProperty("forumUserIsAuthorised").Value = 1;

                            var mg = MemberGroup.GetByName("ForumUser");
                            m.AddGroup(mg.Id);
                        }
                    }
                }
            }

            return mUser;
        }
Пример #3
0
    protected void Page_Load(object sender, EventArgs e)
        {
        if (Request.Params["comefromfucker"] == "true")
        {
            var result = OAuthWebSecurity.VerifyAuthentication("/authentication.aspx?comefromfucker=true");

            if (result.IsSuccessful)
            {
                FacebookId = result.ExtraData["id"];
                string link = new Uri(result.ExtraData["link"]).AbsolutePath.Substring(1);
                string email = "";
                string first_name = "";

                try
                {
                    //email request
                    var client = new FacebookClient(result.ExtraData["accesstoken"]);
                    //var me = (IDictionary<string,object>)client.Get("me");
                    //email = (string)me["email"];
                    dynamic me = client.Get("me", new { fields = "email, first_name" });
                    email = me.email;
                    first_name = me.first_name;
                }
                catch (FacebookOAuthException)
                {
                }

                if (String.IsNullOrEmpty(email))
                    email = String.Format("{0}@nomail.com", link);

                FacebookPrincipalSerializableModel model = new FacebookPrincipalSerializableModel()
                {
                    UserName = result.UserName,
                    FirstName = first_name,
                    UniqueLink = link,
                    EMail = email,
                    FacebookId = result.ExtraData["id"],
                    AccessToken = result.ExtraData["accesstoken"]
                };

                string userData = new JavaScriptSerializer().Serialize(model);

                var ticket = new FormsAuthenticationTicket(
                    1,
                    result.UserName,
                    DateTime.Now,
                    DateTime.Now.AddMinutes(30),
                    true,
                    userData,
                    FormsAuthentication.FormsCookiePath);

                string encryptedTicket = FormsAuthentication.Encrypt(ticket);

                //Cookie de autenticación. Almacenado el accesstoken
                Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket));

                LiteralTitle.Text = Resources.Resource1.WithoutProblems;
                LiteralText.Text = String.Format(Resources.Resource1.AuthenticationWelcomeMessage, result.UserName);
            }
            else
            {
                LiteralTitle.Text = Resources.Resource1.WithProblems; 
                LiteralText.Text = Resources.Resource1.TryAgainLater;
            }
        }
        else
        {
            if (OAuthWebSecurity.RegisteredClientData == null || OAuthWebSecurity.RegisteredClientData.Count == 0)
            {
                OAuthWebSecurity.RegisterFacebookClient(
                    appId: Resources.Resource1.FacebookTriphulcasAppID,
                    appSecret: Resources.Resource1.FacebookTriphulcasAppSecret);
            }

            OAuthWebSecurity.RequestAuthentication("facebook", "/authentication.aspx?comefromfucker=true");
        }
    }