简体   繁体   English

如何在 web api2 的标题中设置和返回会话 ID

[英]How to set and return session id in headers in web api2

After successful login I want to return session_id in response headers and along with that some object I want to return.成功登录后,我想在响应头中返回session_id以及我想返回的一些对象。

My typical response header is as follows.我的典型响应头如下。

Response
Header: Set-cookie: session_id=121212-343dsfsd-4323132, path=/, expires: 1212
Body: {
    “status”: 0,
    “data”: {
        “userRole”: “SUPER_ADMIN”
    }
}

I am trying as below to achieve above.我正在尝试按以下方式实现上述目标。

bool result = //...validate username and password with database

if(result == true)
{
    SessionIDManager manager = new SessionIDManager();
    string newSessionId= manager.CreateSessionID(HttpContext.Current);
    var resp = new HttpResponseMessage();
    var cookie = new CookieHeaderValue("session-id",newSessionId);
    cookie.Expires = DateTimeOffset.Now.AddDays(1);
    cookie.Domain = Request.RequestUri.Host;
    cookie.Path = "/";
    resp.Headers.AddCookies(new CookieHeaderValue[] { cookie });
    //return resp;
    obj.UserRole = (from c in entityObject.NCT_UserRegistration where obj.User_Name == c.User_Name && obj.User_Password == c.User_Password select c.User_Role).FirstOrDefault();
    obj.Success = 0;
    obj.User_Password="";
    return Ok(obj);
}

I am not sure the way I followed correct or not.我不确定我遵循的方式是否正确。 How can I return sessionid as above?我怎样才能像上面一样返回sessionid

Refactor the above code as follows将上面的代码重构如下

if(result == true) {

    obj.UserRole = (from c in entityObject.NCT_UserRegistration where obj.User_Name == c.User_Name && obj.User_Password == c.User_Password select c.User_Role).FirstOrDefault();
    obj.Success = 0;
    obj.User_Password = "";

    var response = Request.CreateResponse(HttpStatusCode.OK, obj);

    var newSessionId = new SessionIDManager().CreateSessionID(HttpContext.Current);
    var cookie = new CookieHeaderValue("session-id", newSessionId);
    cookie.Expires = DateTimeOffset.Now.AddDays(1);
    cookie.Domain = Request.RequestUri.Host;
    cookie.Path = "/";

    response.Headers.AddCookies(new[] { cookie });

    return ResponseMessage(response);
}

Main difference is how the response is created and returned.主要区别在于如何创建和返回响应。 The original code was creating a new response manually, populating it with cookie and then returning another completely different response that had the body minus cookie.原始代码是手动创建一个新的响应,用 cookie 填充它,然后返回另一个完全不同的响应,该响应的主体减去 cookie。 ie: Ok(obj) .即: Ok(obj)

The above code creates a response that includes the object value to be returned and then the cookie header information is added to the response.上面的代码创建了一个包含要返回的对象值的响应,然后将 cookie 头信息添加到响应中。

If the original intention of the OP was to return IHttpActionResult , then ResponseMessage(response) will wrap the HttpResponseMessage in a IHttpActionResult derived implementation.如果 OP 的初衷是返回IHttpActionResult ,那么ResponseMessage(response)会将HttpResponseMessage包装在IHttpActionResult派生实现中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM