C# 如何使用令牌获取有关用户的信息
需要使用令牌接收用户数据。你好需要使用令牌接收用户数据。我有一个web api+websockets,通过web浏览器连接websocketsC# 如何使用令牌获取有关用户的信息,c#,asp.net,asp.net-mvc,asp.net-web-api,websocket,C#,Asp.net,Asp.net Mvc,Asp.net Web Api,Websocket,需要使用令牌接收用户数据。你好需要使用令牌接收用户数据。我有一个web api+websockets,通过web浏览器连接websockets var webSocket = new WebSocket(handlerUrl); //Open connection handler. webSocket.onopen = function () { webSocket.send("{\"type\":\"LOGIN\",\"access_token\":\"Bearer HIDDEN\"}
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>
公共类SocketClientController:ApicController
{
公共HttpResponseMessage获取()
{
HttpContext.Current.AcceptWebSocketRequest(新的WebSocketHandler());
return Request.CreateResponse(HttpStatusCode.SwitchingProtocols);
}
插座类别:
<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;
}
}
私有无效登录(字符串访问\u令牌)
{
//这里我想得到用户信息
}
公共覆盖无效消息(字符串输入)
{
动态数据=JObject.Parse(输入);
开关((字符串)数据类型)
{
案例“登录”:
登录((字符串)data.access\u令牌);
打破
}
}
我使用Identity,当您第一次从客户端收到数据时,它是一个带有令牌的变体。请告诉我您如何在不使用登录名和密码的情况下获得用户输入,并使用[Authorize]
对不起,我的英语不好。我决定了我的任务!下面是代码:
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);
}
}
使用:
我决定了我的任务!下面是代码:
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);
}
}
使用:
您将令牌保存在何处?您必须保存令牌(如会话数据)以验证从用户(登录)处获得的令牌@rbr94谢谢你的回答!我不太明白,在会话中维护令牌不是问题,因为只有令牌才能获得用户名?你的意思是结合用户名和令牌,即两者之间存在某种连接?你将令牌保存在哪里?你必须保存令牌(如会话数据)要验证您从用户(登录)处获得的令牌@rbr94谢谢您的回答!我不太明白,在会话中维护令牌不是问题,因为可以仅获取一个令牌来获取用户名?您的意思是将用户名和令牌结合起来,即它们之间存在某种连接?