简体   繁体   English

MVC 5 Identity 2和Web API 2授权以及使用承载令牌的呼叫api

[英]MVC 5 Identity 2 and Web API 2 authorization and call api using bearer token

The following scenario: I have an MVC 5 web app using Identity 2.0 and Web API 2. 以下场景:我有一个使用Identity 2.0和Web API 2的MVC 5 Web应用程序。

Once the user authenticates in MVC 5 he should be able to call a WEB API endpoint let's call it: api/getmydetails using a bearer token. 一旦用户在MVC 5中进行身份验证,他就应该能够调用WEB API端点,让我们使用承载令牌来调用它: api/getmydetails

What I need to know is how can I issue the token for that specific user in MVC 5? 我需要知道的是如何在MVC 5中为特定用户发出令牌?

I did solve this. 我确实解决了这个问题

Here are some screenshots and I will also post the demo solution. 这是一些截图,我也将发布演示解决方案。

Just a simple mvc 5 with web api support application. 只是一个简单的mvc 5与web api支持应用程序。

The main thing you have to register and after login. 您必须注册和登录后的主要事项。 For this demo purpose I registered as admin@domain.com with password Password123* . 出于演示目的,我注册为admin@domain.com ,密码为Password123*

If you are not logged in you will not get the token. 如果您尚未登录,则无法获得令牌。 But once you loggin you will see the token: 但是一旦你登录你就会看到令牌:

在此输入图像描述

After you get the token start Fiddler. 获得令牌后启动Fiddler。

Make a get request to the api/service endpoint. api/service端点发出get请求。 You will get 401 Unauthorized 您将获得401 Unauthorized

在此输入图像描述

Here is the description of the request: 以下是请求的说明:

在此输入图像描述

Now go to the web app, stage 1 and copy the generated token and add the following Authorization header: Authorization: Bearer token_here please notice the Bearer keyword should be before the token as in the image bellow. 现在转到Web应用程序,第1阶段并复制生成的令牌并添加以下授权标头: Authorization: Bearer token_here请注意Bearer关键字应该在令牌之前,如下图所示。 Make a new request now: 立即提出新请求:

在此输入图像描述

Now you will get a 200 Ok response. 现在你将获得200 Ok的回复。 The response is actually the user id and user name that show's you are authorized as that specific user: 响应实际上是user iduser name ,显示您被授权为该特定用户:

在此输入图像描述

You can download the working solution from here: 您可以从这里下载工作解决方案:

http://www.filedropper.com/bearertoken http://www.filedropper.com/bearertoken

If for some reason the link doesn't work just let me know and I will send it to you. 如果由于某种原因链接不起作用,请告诉我,我会将其发送给您。

PS PS

Of course in your app, you can use the generated bearer token to make ajax call to the web api endpoint and get the data, I didn't do that but should be quite easy ... 当然在你的应用程序中,你可以使用生成的bearer令牌对web api端点进行ajax调用并获取数据,我没有这样做,但应该很容易......

PS 2: To generate the token: PS 2:生成令牌:

   private string GetToken(ApplicationUser userIdentity)
    {
        if (userIdentity == null)
        {
            return "no token";
        }

        if (userIdentity != null)
        {
            ClaimsIdentity identity = new ClaimsIdentity(Startup.OAuthBearerOptions.AuthenticationType);

            identity.AddClaim(new Claim(ClaimTypes.Name, userIdentity.UserName));
            identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userIdentity.Id));

            AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties());

            DateTime currentUtc = DateTime.UtcNow;
            ticket.Properties.IssuedUtc = currentUtc;
            ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30));

            string AccessToken = Startup.OAuthBearerOptions.AccessTokenFormat.Protect(ticket);
            return AccessToken;
        }

        return "no token";
    }

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

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