Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 服务堆栈的基本身份验证大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的 Android应用程序中使用JsonserviceClient(使用Xamerin编写).我有一个测试客户端,它与servicestack网站上给出的HelloWorld示例一起使用.无需身份验证即可正常运行并快速返回值.

现在我正在尝试将身份验证纳入混合,从非常基本的身份验证开始.我在服务器上有一个自定义身份验证和会话类,如下所示:

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 – 服务堆栈的基本身份验证全部内容,希望文章能够帮你解决android – 服务堆栈的基本身份验证所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。