如何注册azure移动服务.net后端自定义api

时间:2015-05-12 13:02:56

标签: java android azure mobile azure-mobile-services

我使用网站article上提供的代码示例在Windows Azure上创建了.NET后端移动服务。 现在我试图用android客户端注册用户,但我不能。

我的后端注册控件如下所示;

 [AuthorizeLevel(AuthorizationLevel.Anonymous)]
public class CustomRegistrationController : ApiController
{
    public ApiServices Services { get; set; }

    // POST api/CustomRegistration
    public HttpResponseMessage Post(RegistrationRequest registrationRequest)
    {
        if (!Regex.IsMatch(registrationRequest.username, "^[a-zA-Z0-9]{4,}$"))
        {
            return this.Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid username (at least 4 chars, alphanumeric only)");
        }
        else if (registrationRequest.password.Length < 8)
        {
            return this.Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid password (at least 8 chars required)");
        }

        hadContext context = new hadContext();
        Account account = context.Accounts.Where(a => a.Username == registrationRequest.username).SingleOrDefault();
        if (account != null)
        {
            return this.Request.CreateResponse(HttpStatusCode.BadRequest, "Username already exists");
        }
        else
        {
            byte[] salt = CustomLoginProviderUtils.generateSalt();
            Account newAccount = new Account
            {
                Id = Guid.NewGuid().ToString(),
                Username = registrationRequest.username,
                Salt = salt,
                SaltedAndHashedPassword = CustomLoginProviderUtils.hash(registrationRequest.password, salt)
            };
            context.Accounts.Add(newAccount);
            context.SaveChanges();
            return this.Request.CreateResponse(HttpStatusCode.Created);
        }
    }

}

我在Android客户端应用程序上编写了这段代码

   public void register(View view) {

       if (   txtUsername.getText().toString().equals("")
           && txtPassword.getText().toString().equals(""))
       {
           Log.w(TAG,"tüm alanları girmen gerek");
           return;
       }
    else
       {
           RegistrationRequest register = new RegistrationRequest();
           register.setUsername(txtUsername.getText().toString());
           register.setPassword(txtUsername.getText().toString());
           mClient.invokeApi("CustomRegistration",register,RegistrationRequest.class,
                   new ApiOperationCallback<RegistrationRequest>() {
                       @Override
                       public void onCompleted(RegistrationRequest result, Exception exception, ServiceFilterResponse response) {
                          if (exception==null)
                          {
                              Log.w(TAG,"kayıt başarılı");
                          }
                           else
                          {
                              Log.w(TAG,"kayıt başarısız " +exception);
                          }
                       }
                   });
       }

}

它不起作用。我应该如何进行注册。

0 个答案:

没有答案