简体   繁体   English

如何在我的REST控制器中获取Bearer令牌

[英]How to get the Bearer Token in my REST controller

Hello (sorry for my bad english) 你好(对不起,我的英语不好)

I have an Angular Vue.js front that connect to my Rest Api Services in C# . 我有一个Angular Vue.js前端,该前端连接到C#中的Rest Api服务。 I have at least 20 services developed. 我至少开发了20项服务。 Now I want to secure them. 现在,我要保护它们。

With POSTMAN and the url https://login.microsoftonline.com ... I get the "Token" , then I try to get the authorization and I put hardCode the given token , and the result its: "StatusCode": 200, "ReasonPhrase": "Authorized", 使用POSTMAN和url https://login.microsoftonline.com ...我得到“令牌”,然后尝试获得授权,然后将给定的令牌放入hardCode中,其结果是:“ StatusCode”:200, “ ReasonPhrase”:“已授权”,

I do something like this: 我做这样的事情:

    public class TokenController : ApiController
    {
        public IHttpActionResult Get()
        {
            Task<HttpResponseMessage> d = TokenManager.GetGroups("token");
            return Ok(d);
        }
    }


    public static Task<HttpResponseMessage> GetGroups(string token)
    {
        var httpClient = new HttpClient();

        var uri = "https://graph.microsoft.com/v1.0/groups";
        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

        var res = httpClient.GetAsync(uri, HttpCompletionOption.ResponseContentRead).Result;

        return Task.Delay(3000).ContinueWith(t => res);
    }

My problem its that I don´t know where or what I have to do to Get the "Bearer Token" from the "Front". 我的问题是我不知道从“前端”获取“承载者令牌”的位置或要做的事情。 I can't get the token by parameter in the URL of the service because is to long. 由于太长,无法通过服务的URL中的参数获取令牌。

Thank you. 谢谢。

With Request.Headers I have the Bearer Token. 有了Request.Headers,我有了Bearer令牌。

I call "string tt = Request.Headers.Authorization.ToString();" 我称“字符串tt = Request.Headers.Authorization.ToString();” in my controller and it works fine 在我的控制器中,它工作正常

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

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