簡體   English   中英

如何使用令牌獲取有關用戶的信息

[英]How can I get information about the user using a token

需要使用令牌接收用戶數據。 你好。 需要使用令牌接收用戶數據。 我有一個web api + websockets,通過Web瀏覽器連接websockets。

var webSocket = new WebSocket(handlerUrl);
//Open connection  handler.
webSocket.onopen = function () {
   webSocket.send("{\"type\":\"LOGIN\",\"access_token\":\"Bearer HIDDEN\"}");
};

連接后,我立即發送令牌。 在服務器端,它看起來如下:

 public class SocketClientController: ApiController
 {
     public HttpResponseMessage Get ()
     {
        HttpContext.Current.AcceptWebSocketRequest (new WebSocketHandler ());
        return Request.CreateResponse (HttpStatusCode.SwitchingProtocols);
     }
 <miss>

套接字類:

    <miss>
     private void Login(string access_token)
     {
// here i want get user info
     }

     public override void OnMessage(string input)
     {
           dynamic data = JObject.Parse(input);

           switch ((string)data.type)
           {
              case "LOGIN":
                Login((string)data.access_token);
              break;
           }
     }

我使用Identity,一個帶有令牌的變體,當你第一次從客戶端收到適合我的數據時。 告訴我如何在不通過登錄名和密碼的情況下獲取用戶輸入,並使用[授權]。

對不起我的英語不好。

我決定了我的任務! 以下是代碼:

  public class MachineKeyProtector : Microsoft.Owin.Security.DataProtection.IDataProtector
        {
            private readonly string[] _purpose =
            {
                    typeof(OAuthAuthorizationServerMiddleware).Namespace,
                    "Access_Token",
                    "v1"
                };

            public byte[] Protect(byte[] userData)
            {
                throw new NotImplementedException();
            }

            public byte[] Unprotect(byte[] protectedData)
            {
                return System.Web.Security.MachineKey.Unprotect(protectedData, _purpose);
            }
        }

采用:

 var secureDataFormat = new TicketDataFormat(new Providers.MachineKeyProtector());
 AuthenticationTicket ticket = secureDataFormat.Unprotect(access_token);
 var userId = ticket.Identity.GetUserId();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM