Authentication MonoTouch如何在每个ServiceStack请求上提供cookie?
我花了几天的时间试图掌握ServiceStack,它看起来很棒。唯一的问题是认证,这似乎是一个摩擦,艰苦的工作和眼泪很多 我希望MonoTouch注册用户,根据ServiceStack进行身份验证/根据OAuth进行身份验证,并在身份验证时尽量减少对数据库的调用 到目前为止,我已经做到了:Authentication MonoTouch如何在每个ServiceStack请求上提供cookie?,authentication,cookies,xamarin.ios,
servicestack,xamarin,Authentication,Cookies,Xamarin.ios,
servicestack,Xamarin,我花了几天的时间试图掌握ServiceStack,它看起来很棒。唯一的问题是认证,这似乎是一个摩擦,艰苦的工作和眼泪很多 我希望MonoTouch注册用户,根据ServiceStack进行身份验证/根据OAuth进行身份验证,并在身份验证时尽量减少对数据库的调用 到目前为止,我已经做到了: var client = new JsonServiceClient(newbaseUri); // register a new user: var registratio
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));
var client=newjsonserviceclient(newbaseUri);
//注册新用户:
var注册=新注册{
FirstName=“约翰”
UserName=“user”,
Password=“pass”,
电子邮件=”john@john.com",
};
var registerResponse=client.Send(注册);
--------------------------------
//用户已注册…稍后我将进行身份验证:
var authResponse=client.Send(新身份验证{
UserName=“user”,
Password=“pass”,
RememberMe=true
});
var authResponse=clientlogin.Send(auth);
--------------------------------
//不知何故,我需要为以后的调用存储“authresponse”,不知道在没有浏览器的情况下如何存储
//稍后尝试手动设置Cookie和凭据参数,但没有成功
//但现在我需要调用一个安全([Authenticate]修饰)服务方法:
var client=newjsonserviceclient(newbaseUri);
var response=client.Send(newhello{Name=“World!”);
返回响应。结果;
-----------------------------------------
//这是配置
var appSettings=新的appSettings();
//默认路由:/auth/{provider}
添加(新的AuthFeature(()=>新的CustomUserSession(),
新IAuthProvider[]{
new CredentialAuthProvider(appSettings),//似乎从未被调用
//新建FacebookAuthProvider(appSettings),//不确定如何在monotouch上运行此功能
//新TwitterAuthProvider(appSettings),//与facebook的问题相同
新的BasicAuthProvider(appSettings)//可以工作,但是缓存/令牌/cookie呢?
}));
//默认路由:/register
Plugins.Add(新注册功能());//如何将在mongodb集合中创建的额外参数发送到此文件
var mongoClient=新的mongoClient(“mongodb://localhost");
var server=mongoClient.GetServer();
var db=server.GetDatabase(“用户”);
Register(newmemorycacheclient());
容器注册(新MongoDBAuthRepository(db,true));
我的问题是:
1) 我如何允许在注册时传入额外字段(因为mongodb[Servicestack.Authentication.mongodb]有很多空字段,例如生日、第一行、城市、时区等),这些字段在Servicestack.Common.ServiceClient.Web.registration对象中不存在
2) 如何将“authresponse”中发送的cookie(甚至可能是令牌系统)传输到后续调用,以允许ServiceStack与会话匹配以进行持续身份验证,而不是更多的持续数据库调用,这似乎是“基本身份验证”方法的问题(即CredentialAuthProvider不会在服务器端被调用)
请帮助…我已经阅读了文档,运行了测试,检查了社会引导,现在我已经为此浪费了好几天的时间,并且正在考虑将SS与simplemembership集成,甚至将ServiceStack完全扔掉,换成旧的skool soap/wcf,从外观上看,它更容易实现:(1)如果你想使用注册插件,我不认为你可以添加其他字段,因为注册请求/类已经定义。你可以创建自己的注册服务并调用RegistrationService/插件。另外,可能会有帮助
[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();
}
}
Ok在访问另一个方法(Q2)之前,通过将响应对象的cookiecontainer复制到下一个请求的JsonServiceClient,成功地使Cookie正常工作……但Q1仍然没有得到响应:(感谢paaschpa!我通过使用ServiceStack github repo中的CustomAuthenticate示例并将cookiecontainer从一个请求复制到另一个请求(类似于您所描述的内容),使cookie正常工作).读了你的答案后,我现在对注册过程有了更好的了解-我会尝试并再次访问此线程进行报告…非常感谢..我再次抱有希望:我在创建iPad原型应用程序时遇到了同样的问题。我打算将ss id存储在iOS沙盒文件系统中的一个文件中,所以当你重新访问应用程序时您可以检索cookie并将其添加到容器中。
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!" });
//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));
}
};