c# - 在 asp.net MVC4 应用程序中重新连接到 Servicestack session

标签 c# session asp.net-mvc-4 authentication servicestack

我有一个 asp.net mvc4 web 应用程序正在使用来自用 C# 编写并托管在带有 Apache/mod_mono 的 Linux 机器上的 API 的数据

客户端应用程序是用 C#/asp.net 编写的 - 它运行在不同的 Web 服务器上,也是 Linux/Apache/mod_mono。我不确定这些细节在这种情况下是否重要,但我认为任何背景知识都可能有所帮助。

导致这个问题的问题:AppHostBase instance not set - 帮助我对这一切如何组合在一起有了更多的了解。

我认为我现在应该问的正确问题是:一旦我在 servicestack(在 API 服务器上)中创建了一个 session ,我该如何正确地重新连接到它?

根据前面问题的答案,我在客户端应用程序的身份验证 Controller 中使用了这段代码:

        var authService = AppHostBase.Resolve<AuthService>();
        authService.RequestContext = System.Web.HttpContext.Current.ToRequestContext();
        var AuthResponse = authService.Authenticate(new Auth
            {
                provider = "credentials",
                UserName = user.user_id,
                Password = user.password,
                RememberMe = true
            });

这将返回一个 ResolutionException:
无法解析 ServiceStack.ServiceInterface.Auth.AuthService 类型的必需依赖项。

在让客户端在 asp.net 应用程序中工作时,我可能会遗漏一些简单的东西吗?

如果问题太模糊,我深表歉意,很乐意提供更多信息。

更新:
这是 AuthController - 请原谅,自从我上一篇文章以来我一直在尝试一些事情:

{
public partial class AuthController : BaseController
{
    JsonServiceClient client = new ServiceStack.ServiceClient.Web.JsonServiceClient("<TheAPIurl>");

    // GET: /Login/

    public ActionResult login()
    {
        if (Session["IsAuthenticated"] != null)
        {
            ViewData["Result"] = Session["IsAuthenticated"];
        }

        return View();
    }

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult login(UserModel user)
    {
        try
        {
        var authService = AppHostBase.Resolve<AuthService>();
        authService.RequestContext = System.Web.HttpContext.Current.ToRequestContext();
        var AuthResponse = authService.Authenticate(new Auth
            {
                provider = "credentials",
                UserName = user.user_id,
                Password = user.password,
                RememberMe = true
            });

            if (AuthResponse.SessionId != null)
            {
                Session["IsAuthenticated"] = true;
                Session["UserID"] = AuthResponse.UserName;
                Session["jsclient"] = client; 
                FormsAuthentication.SetAuthCookie(user.user_id, true);
                return Redirect("/default");
            }
            else
            {
                Session["IsAuthenticated"] = false;
            }

        }
        catch (Exception ex)
        {
            Session["IsAuthenticated"] = false;
        }

        return View();
    }
    protected override void ExecuteCore()
    {
        throw new NotImplementedException();
    }
}

最佳答案

使用本地 ServiceStack 实例进行身份验证

如果 ServiceStack 实例托管在与 MVC 相同的应用程序域中,则您只能从 ServiceStack 容器中检索自动连接的 ServiceStack 服务(或其他 IOC 依赖项),即:

var authService = AppHostBase.Resolve<AuthService>();
authService.RequestContext = System.Web.HttpContext.Current.ToRequestContext();

虽然推荐的解决另一个服务的自动连接实现的代码是:

using (var authAservice = AppHostBase.ResolveService<AuthService>()) {
    ...
}

即因为服务可能会使用应该被释放的资源。在 ServiceStack 服务中你应该使用 base.ResolveService<AuthService>()相反。

因此,如果 ServiceStack 托管在与 MVC 相同的 AppDomain 中,您可以调用服务目录,如下所示:

var authResponse = authService.Authenticate(new Auth {
    provider = "credentials",
    UserName = user.user_id,
    Password = user.password,
    RememberMe = true
});

使用远程 ServiceStack 实例进行身份验证

否则,如果它是远程的,您需要使用 ServiceStack's C# Service Clients 之一,例如:

var client = new JsonServiceClient(ServiceStackBaseUrl);
var authResponse = client.Post(new Auth {
    provider = "credentials",
    UserName = user.user_id,
    Password = user.password,
    RememberMe = true
});

将 ServiceStack SessionId 附加回原始 MVC 请求

这将通过将其附加到 ss-pid 来设置与该 ServiceClient 客户端的经过身份验证的 session Cookie(有关更多信息,请参阅 Session docs)。您可以将此 cookie 传递给调用 MVC 的原始浏览器:

var response = HttpContext.Current.Response.ToResponse();
response.Cookies.AddSessionCookie(
    SessionFeature.PermanentSessionId, authResponse.SessionId);

经过身份验证的 session 的后续请求

要从 MVC 中重新连接远程经过身份验证的 ServiceStack session ,您需要将 cookie 传回服务客户端,例如:

var cookie = HttpContext.Request.Cookies.Get(SessionFeature.PermanentSessionId);

var client = new JsonServiceClient(ServiceStackBaseUrl);
var cookie = new Cookie(SessionFeature.PermanentSessionId, cookie.Value);
client.CookieContainer.Add(cookie);

您可以在 Web.Config 中全局设置 cookie 域:

<httpCookies domain="mydomain.com" />

或者在运行时使用:

cookie.Domain = "mydomain.com";

ServiceStack AuthTests.cs Integration Tests还有一些其他有用的示例展示了身份验证在 ServiceStack 中的工作原理。

关于c# - 在 asp.net MVC4 应用程序中重新连接到 Servicestack session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19160839/

相关文章:

html - 如何在 html razor view mvc4 中增加 TextArea 的大小?

python - 全局名称 'request' 未定义

php - 如何限制 Laravel 4 中每个用户帐户的同时 session 数

php - session_regenerate_id() 与 session_id(randomString)

visual-studio-2010 - ASP.NET MVC 4 破坏了 ASP.NET MVC 3 项目

c# - .NET HttpListener 前缀问题与本地主机以外的任何东西

c# - 管理 NuGet 包将 jquery 恢复到旧版本

javascript - 如何使用字符串数组调用 JavaScript 函数

c# - 在凭据管理器中访问 Windows 凭据

asp.net - 在 ASP.NET MVC 4 中使用区域时出现配置错误