如何可以在每个ServiceStack要求MonoTouch的供应饼干吗? [英] How can MonoTouch supply cookie on each ServiceStack request?

查看:119
本文介绍了如何可以在每个ServiceStack要求MonoTouch的供应饼干吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了好几天试图获得与ServiceStack交手,这似乎很大。唯一的问题是身份验证这似乎是很多摩擦,努力和泪水。

I've spent several days attempting to get to grips with ServiceStack and it seems great. Only issue is with authentication which seems to be a lot of friction, hard work and tears.

我要MonoTouch的注册用户,针对ServiceStack认证/对OAuth的认证,一般尽量减少身份验证时,调用数据库。

I want MonoTouch to register users, authenticate against ServiceStack/authenticate against OAuth and generally minimise calls to the database when authenticating.

到目前为止,我已经得到了这一点:

So far I have got this:

       var client = new JsonServiceClient(newbaseUri);

// register a new user:

        var registration  = new Registration {
            FirstName = "john"
            UserName = "user" ,
            Password = "pass",
            Email =   "john@john.com",              
        };

        var registerResponse = client.Send<RegistrationResponse>(registration);

       --------------------------------

// user registered...later on I authenticate:

        var authResponse = client.Send<AuthResponse>(new Auth {
            UserName = "user",
            Password = "pass",
            RememberMe = true
        });

        var authResponse = clientlogin.Send<AuthResponse>(auth);

        --------------------------------    

// somehow I need to store 'authresponse' for later calls, not sure how without a browser 
// tried manually setting the cookies and credentials parameters later but no joy
// but now I need to call a secured ([Authenticate] decorated) service method:

        var client = new JsonServiceClient(newbaseUri);
        var response = client.Send<HelloResponse>(new Hello { Name = "World!" });           
        return response.Result;

-----------------------------------------

// heres the configuration

        var appSettings = new AppSettings();

        //Default route: /auth/{provider}
        Plugins.Add(new AuthFeature(() => new CustomUserSession(),
            new IAuthProvider[] {
                new CredentialsAuthProvider(appSettings),  // never seems to get called
                //new FacebookAuthProvider(appSettings),    // not sure how to get this to work on monotouch
                //new TwitterAuthProvider(appSettings),    // same issue as facebook
                new BasicAuthProvider(appSettings)    // works but what about caching/tokens/cookies?
            }));

        //Default route: /register
        Plugins.Add(new RegistrationFeature());    // how do i send extra params to this as created in mongodb collection


        var mongoClient = new MongoClient("mongodb://localhost");
        var server = mongoClient.GetServer();
        var db = server.GetDatabase("users");

        container.Register<ICacheClient>(new MemoryCacheClient());
        container.Register<IUserAuthRepository>(new MongoDBAuthRepository(db, true));

我的问题是:

1)如何启用额外的字段一起传递与登记(如MongoDB的[Servicestack.Authentication.Mongodb]有很多空的字段,即出生日期,FIRSTLINE,城市,时区等),不属于$ P在ServiceStack.Common.ServiceClient.Web.Registration对象$ psent?

1) How do I enable extra fields to be passed in along with registration (as the mongodb [Servicestack.Authentication.Mongodb] has lots of empty fields i.e. birthdate, firstline, city, timezone, etc) that are not present in ServiceStack.Common.ServiceClient.Web.Registration object?

2)我怎么能转移的cookie(甚至可能是令牌系统),以便在authresponse随后话费送允许ServiceStack来匹配正在进行的认证会话,而不是更多的持续的数据库调用,似乎什么要与基本身份验证的方法问题(即CredentialsAuthProvider犯规被调用服务器端)?

2) How can I transfer the cookie (or even maybe a token system) sent in the 'authresponse' to subsequent calls in order to allow ServiceStack to match against the session for ongoing authentication rather than more ongoing database calls that what seems to be issue with 'basic authentication' method (i.e CredentialsAuthProvider doesnt get called on server side)?

请帮忙...我已经阅读文档,运行测试,检查社会引导和超过这个现在我认真失去天,整合与SS或simplemembership扔甚至离开ServiceStack完全老斯库尔SOAP / WCF的思考哪些要容易得多落实它的外观:(

Please help...I've read documentation, run tests, examined social bootstrap and now I'm seriously losing days over this and thinking of integrating SS with simplemembership or even throwing ServiceStack away completely for old skool soap/wcf which is far easier to implement by the looks of it :(

推荐答案

1)如果你想使用的注册插件,我不认为你可以自注册请求添加其他字段/类已定义。你可以让你自己的注册服务,并调入RegistrationService /插件。此外,这个帖子可能会有所帮助。

1) If you want to use the Registration Plugin I don't think you can add additional fields since the Registration request/class is already defined. You could make your own registration Service and call into the RegistrationService/Plugin. Also, this post might be helpful.

[Route("/myregistration")]
public class MyRegistration : Registration //Add Additional fields for registration
{
    public DateTime? BirthDate { get; set;  }
    public string Gender { get; set; } 
}

public class MyRegisterService : Service
{
    public IUserAuthRepository UserAuthRepo { get; set; }
    public object Post(MyRegistration request)
    {
        using (var registrationService = base.ResolveService<RegistrationService>())
        {
            //handle the registration 
            var response = registrationService.Post(request.TranslateTo<Registration>());
        }

        //save the additional data
        var userAuth = request.TranslateTo<UserAuth>();
        UserAuthRepo.SaveUserAuth(userAuth);

        //can make your own response or grab response from RegistrationService above    
        return new MyRegistrationResponse();
    }
}

2)您可以验证您的JsonServiceClient和再利用它来使多个请求。

2) You can authenticate your JsonServiceClient and reuse it to make multiple requests.

var client = new JsonServiceClient(newbaseUri);
var authResponse = client.Send<AuthResponse>(new Auth {
    UserName = "user",
    Password = "pass",
    RememberMe = true
}); //if successful your 'client' will have a populated CookieContainer with 'ss-id' and 'ss-pid' values

//reusing 'client' (after successful authentication) to make a request
//to a service requiring authentication
var response = client.Send<HelloResponse>(new Hello { Name = "World!" });

如果您重用'客户'是不是你可以尝试储存SS-ID的选项。我不知道很多关于MonoTouch的,以及它如何店'浏览器会话',所以我不知道你将如何做到这一点。当您进行身份验证并存储SS-ID,您可以使用过滤器请求其添加到客户端

If reusing your 'client' is not an option you can try to store the ss-id. I don't know much about MonoTouch and how it stores 'browser sessions' so I'm not sure how you would accomplish this. After you authenticate and store the ss-id you can add it to the client using a Request Filter

//Get ss-id value
foreach(Cookie cookie in previousAuthenticatedClient.GetCookies(new Uri(newbaseUri)))
{
    if (cookie.Name == "ss-id") 
    {
        //store ss-id 
    }
}

var newClient = new JsonServiceClient(newbaseUri)
{
    LocalHttpWebRequestFilter = (req) =>
        {
            req.CookieContainer.Add(new Uri("http://localhost:56006"), new System.Net.Cookie("ss-id", ssId));
        }
};

这篇关于如何可以在每个ServiceStack要求MonoTouch的供应饼干吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆