我在我的 Android 应用程序(使用 Xamerin 编写)中使用 JsonServiceClient。我有一个测试客户端,可以与 servicestack 网站上提供的 HelloWorld 示例一起使用。它无需身份验证即可正常工作并快速返回值。
现在我正尝试将身份验证纳入其中,从非常基本的身份验证开始。我在服务器上有一个自定义的身份验证和 session 类,如下所示:
public class userSession : AuthUserSession
{
public string clientCode { get; set; }
}
public class userAuth : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
if (userName == "user" || password == "1234") {
var session = (userSession)authService.GetSession(false);
session.clientCode = "peruse";
return true ;
} else {
return false;
}
}
}
配置为:
// auth feature and session feature
Plugins.Add(new AuthFeature(
() => new userSession(),
new[] { new userAuth() }
) { HtmlRedirect = null } );
在客户端,我调用了一个新的 JsonServerClient:
JsonServiceClient client = new ServiceStack.ServiceClient.Web.JsonServiceClient("http://172.16.0.15/");
Android 界面上的按钮事件:
try
{
client.SetCredentials("user", "1234");
HelloResponse response = client.Get<HelloResponse>("/hello/" + toSum.Text);
txtResult.Text = response.Result ;
}
catch (Exception ex)
{
txtResult.Text = ex.Message;
}
我一直从服务器返回 404。当我尝试从 Linux 使用 cURL 访问它时:
curl -v http://user:1234@172.16.0.15/hello/5
它返回:
* Trying 172.16.0.15... connected
* Server auth using Basic with user 'user'
> GET /hello/5 HTTP/1.1
> Authorization: Basic dXNlcjoxMjM0
(其他冗长的东西……然后……)
HTTP/1.1 302 Found
连同看起来像登录页面的链接:
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="/login.aspx?ReturnUrl=%2fhello%2f5">here</a></h2>
</body><html>
我已经进入 Web.config 并删除了对此登录页面的任何引用,但它仍然试图将我发送到那里。
所以我的问题是:我发送凭据的方式是否正确?如果是这样,所提供的代码是否以合理的方式处理它们?
谢谢
最佳答案
您似乎遇到了与此海报相同的问题:ServiceStack Web Service with Basic Authentication and SetCredentials他设法使用以下代码进行身份验证:
class Program
{
const string BaseUrl = "http://localhost:8088/api";
static void Main(string[] args)
{
var restClient = new JsonServiceClient(BaseUrl);
restClient.SetCredentials("john", "test");
restClient.AlwaysSendBasicAuthHeader = true;
HelloResponse response = restClient.Get<HelloResponse>("/hello/Leniel");
Console.WriteLine(response.Result);
}
}
//Response DTO
//Follows naming convention
public class HelloResponse
{
public string Result { get; set; }
}
我建议阅读整个问题和答案,因为它包含详细的解释。
关于android - 服务堆栈的基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18900932/
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
最近,当我启动我的Rails服务器时,我收到了一长串警告。虽然它不影响我的应用程序,但我想知道如何解决这些警告。我的估计是imagemagick以某种方式被调用了两次?当我在警告前后检查我的git日志时。我想知道如何解决这个问题。-bcrypt-ruby(3.1.2)-better_errors(1.0.1)+bcrypt(3.1.7)+bcrypt-ruby(3.1.5)-bcrypt(>=3.1.3)+better_errors(1.1.0)bcrypt和imagemagick有关系吗?/Users/rbchris/.rbenv/versions/2.0.0-p247/lib/ru
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser