Asp.net mvc 为什么mvc控制器中的用户会话为空?
我的mvc项目中有ServiceStack,我正在尝试在ServiceStack和ASP mvc之间共享会话。。我按照中的所有步骤共享会话,但当我尝试在asp mvc控制器中获取UserSession的值时,它会显示一个空值…为什么为空?我有这个密码 AppHost.csAsp.net mvc 为什么mvc控制器中的用户会话为空?,asp.net-mvc,controller,
servicestack,Asp.net Mvc,Controller,
servicestack,我的mvc项目中有ServiceStack,我正在尝试在ServiceStack和ASP mvc之间共享会话。。我按照中的所有步骤共享会话,但当我尝试在asp mvc控制器中获取UserSession的值时,它会显示一个空值…为什么为空?我有这个密码 AppHost.cs { ControllerBase<CustomUserSession> public class CustomUserSession : AuthUserSession {
{
ControllerBase<CustomUserSession>
public class CustomUserSession : AuthUserSession
{
public string CustomProperty1 { get; set; }
public string CustomProperty2 { get; set; }
}
public class AppHost
: AppHostBase
{
public AppHost() //Tell ServiceStack the name and where to find your web services
: base("StarterTemplate ASP.NET Host", typeof(HelloService).Assembly) { }
public override void Configure(Funq.Container container)
{
Plugins.Add(new SessionFeature());
container.Register<ICacheClient>(new MemoryCacheClient());
//Set JSON web services to return idiomatic JSON camelCase properties
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
//Configure User Defined REST Paths
Routes
.Add<Hello>("/hello")
.Add<Hello>("/hello/{Name*}");
//Uncomment to change the default ServiceStack configuration
//SetConfig(new EndpointHostConfig {
//});
//Enable Authentication
//ConfigureAuth(container);
//Register all your dependencies
container.Register(new TodoRepository());
//Set MVC to use the same Funq IOC as ServiceStack
ControllerBuilder.Current.SetControllerFactory(new FunqControllerFactory(container));
ServiceStackController.CatchAllController = reqCtx => container.TryResolve<HomeController>();
}
/* Uncomment to enable ServiceStack Authentication and CustomUserSession
private void ConfigureAuth(Funq.Container container)
{
var appSettings = new AppSettings();
//Default route: /auth/{provider}
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new CredentialsAuthProvider(appSettings),
new FacebookAuthProvider(appSettings),
new TwitterAuthProvider(appSettings),
new BasicAuthProvider(appSettings),
}));
//Default route: /register
Plugins.Add(new RegistrationFeature());
//Requires ConnectionString configured in Web.Config
var connectionString = ConfigurationManager.ConnectionStrings["AppDb"].ConnectionString;
container.Register<IDbConnectionFactory>(c =>
new OrmLiteConnectionFactory(connectionString, SqlServerDialect.Provider));
container.Register<IUserAuthRepository>(c =>
new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>()));
var authRepo = (OrmLiteAuthRepository)container.Resolve<IUserAuthRepository>();
authRepo.CreateMissingTables();
}
*/
public static void Start()
{
new AppHost().Init();
}
}
}
{
控制器基
公共类CustomUserSession:AuthUserSession
{
公共字符串CustomProperty1{get;set;}
公共字符串CustomProperty2{get;set;}
}
公共类AppHost
:AppHostBase
{
public AppHost()//告诉ServiceStack名称以及在哪里可以找到web服务
:base(“启动模板ASP.NET主机”,typeof(HelloService.Assembly){}
公共覆盖无效配置(Funq.Container)
{
Add(newsessionfeature());
Register(newmemorycacheclient());
//设置JSON web服务以返回惯用JSON用例属性
ServiceStack.Text.JsConfig.casenames=true;
//配置用户定义的REST路径
路线
.Add(“/hello”)
.Add(“/hello/{Name*}”);
//取消注释以更改默认ServiceStack配置
//SetConfig(新端点主机配置){
//});
//启用身份验证
//ConfigureAuth(容器);
//注册所有依赖项
container.Register(newtodorepository());
//将MVC设置为使用与ServiceStack相同的Funq IOC
ControllerBuilder.Current.SetControllerFactory(新的FunqControllerFactory(container));
ServiceStackController.CatchAllController=reqCtx=>container.TryResolve();
}
/*取消注释以启用ServiceStack身份验证和CustomUserSession
私有void ConfigureAuth(Funq.Container)
{
var appSettings=新的appSettings();
//默认路由:/auth/{provider}
添加(新的AuthFeature(()=>新的CustomUserSession(),
新IAuthProvider[]{
新的CredentialAuthProvider(应用设置),
新的FacebookAuthProvider(appSettings),
新TwitterAuthProvider(appSettings),
新的BasicAuthProvider(应用设置),
}));
//默认路由:/register
Add(newregistrationfeature());
//需要在Web.Config中配置ConnectionString
var connectionString=ConfigurationManager.connectionString[“AppDb”].connectionString;
容器。寄存器(c=>
新的OrmLiteConnectionFactory(connectionString,SQLServerDialogue.Provider));
容器。寄存器(c=>
新的OrmLiteAuthRepository(c.Resolve());
var authRepo=(OrmLiteAuthRepository)container.Resolve();
authRepo.CreateMissingTables();
}
*/
公共静态void Start()
{
新建AppHost().Init();
}
}
}
HomeController.com
public class HomeController : ControllerBase
{
public virtual ActionResult Index()
{
ViewBag.Message = "Sharing Sessions Btw SS and ASP MVC";
return View();
}
[HttpGet]
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(User request)
{
var user_Session = SessionFeature.GetOrCreateSession<CustomUserSession>(CacheClient);
return Json(user_Session);
}
公共类HomeController:ControllerBase
{
公共虚拟操作结果索引()
{
ViewBag.Message=“共享会话Btw SS和ASP MVC”;
返回视图();
}
[HttpGet]
公共操作结果登录()
{
返回视图();
}
[HttpPost]
公共操作结果登录(用户请求)
{
var user_Session=SessionFeature.GetOrCreateSession(CacheClient);
返回Json(用户会话);
}
因此,用户会话为空…您能帮助我吗?尝试从继承,例如:
嗨,我试过了,但还是得到了空值@mythz@rr7对我来说很好。确保你已经阅读并遵循了指南。已经检查了“遵循每一步”并且仍然得到空值@mythz@rr7我已经添加了一个关于如何告诉ServiceStack您正在使用CustomUserSession的说明。@rr7它在您部分的注释代码中,即
var-appSettings=new AppSettings()
,它只在web.config的
中查找。您也可以使用var AppSettings=new DictionarySettings()
,它只在字符串字典中查找设置。
public class HomeController : ServiceStackController<CustomUserSession>
{
[HttpPost]
public ActionResult Login(User request)
{
CustomUserSession userSession = base.UserSession;
return Json(userSession);
}
}
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new CredentialsAuthProvider(appSettings),
}));