예제 #1
0
파일: Login.cs 프로젝트: OSRS/Oncor_Base
        private void SignUp(HttpContext context, CancellationToken cancel)
        {
            string url = this.ActivateUrl;

            if (!string.IsNullOrEmpty(url))
            {
                IQueryCollection qry = context.Request.Query;
                string           ty  = qry[type];
                if (knownType.Equals(ty))
                {
                    string u = qry[user];
                    if (!string.IsNullOrEmpty(u))
                    {
                        string p = qry[pass];
                        if (!string.IsNullOrEmpty(p))
                        {
                            if (ValidUserEmail(u))
                            {
                                IIdentityProvider idProv = IdentityManager.Instance.GetProvider(ctx);
                                if (!idProv.Exists(u))
                                {
                                    IAuthenticationProvider authProv = AuthenticationManager.Instance.GetProvider(ctx);
                                    UserIdentityBase        user     = idProv.CreateUser(u);
                                    user.UserState = UserState.Pending;
                                    idProv.Update(user);
                                    UserPasswordCredential cred = new UserPasswordCredential(u, p);
                                    if (authProv.AddCredential(user, cred))
                                    {
                                        Guid token = Authenticator.Instance.Reset(u, false); //create a reset token
                                        //notice we create a url with the token at the end, this COULD map to the REST api directly - but is expected instead not to
                                        //we instead expect this to be a simple page that makes the rest request and "looks pretty" to confirm and perhaps send the user then back to the signin page.
                                        if (url.EndsWith("?"))
                                        {
                                            url = url + token.ToString();
                                        }
                                        else
                                        {
                                            url = url + "?" + token.ToString();
                                        }

                                        if (SendEmail(u, url, false))
                                        {
                                            RestUtils.Push(context.Response, JsonOpStatus.Ok);
                                            return;
                                        }
                                        else
                                        {
                                            idProv.Delete(user.Uid);
                                            authProv.DeleteCredential(user, cred);
                                            RestUtils.Push(context.Response, JsonOpStatus.Failed, "\"Couldn't send email\"");
                                            return;
                                        }
                                    }
                                    else
                                    {
                                        idProv.Delete(user.Uid);
                                        authProv.DeleteCredential(user, cred);
                                        RestUtils.Push(context.Response, JsonOpStatus.Failed, "\"Couldn't set credential\"");
                                        return;
                                    }
                                }
                                else
                                {
                                    RestUtils.Push(context.Response, JsonOpStatus.Failed, "\"UserExists\"");
                                    return;
                                }
                            }
                            else
                            {
                                RestUtils.Push(context.Response, JsonOpStatus.Failed, "\"InvalidEmail\"");
                                return;
                            }
                        }
                    }
                }
            }
            RestUtils.Push(context.Response, JsonOpStatus.Failed);
        }