如何在MVC应用程序中使用JWT进行身份validation和授权?
我计划在asp.net MVC应用程序中使用asp.net Identity 2.0进行身份validation和授权。
参考以下链接
使用Owin的ASP.NET Web API 2中的JSON Web令牌
我能够为有效用户创建访问令牌(JWT),即,当用户登录到应用程序时,我将使用名称和密码validation用户,然后我将为该有效用户发出JSON Web令牌。
现在,我在一些文章中读到,我们需要在标头中为每个请求传递承载令牌,以validation用户的身份validation。 在MVC中,我们将为需要保护的方法提供Authorize属性,如下所示…
public class UserController : BaseHRAppController { [Authorize] public ActionResult Index() { return View(); } }
如何告诉我的MVC应用程序使用JWTvalidation用户。
我想让我的MVC应用程序在用户尝试使用authorize属性访问方法时使用JWTvalidation用户。 因为,我将在许多页面中使用ajax调用来访问mvc控制器中存在的方法,我不认为它是在每个ajax请求上传递令牌的好方法。 我需要一个帮助,以便在MVC应用程序中使用asp.net身份以有效的方式完成身份validation和授权。
目前,我不知道如何在mvc应用程序中使用此JWT令牌进行身份validation和授权。 任何帮助表示赞赏谢谢。
为了让MVC了解你的JWT,你基本上要告诉它:-)。 首先,从nuget安装Jwt包:
Install-Package Microsoft.Owin.Security.Jwt
然后打开你的Startup.cs文件并添加一个新的function,告诉MVC如何使用JWT。 在基础知识上,您的Startup将如下所示:
using System.Configuration; using Microsoft.Owin; using Microsoft.Owin.Security; using Microsoft.Owin.Security.DataHandler.Encoder; using Microsoft.Owin.Security.Jwt; using Owin; [assembly: OwinStartupAttribute(typeof(TOMS.Frontend.Startup))] namespace TOMS.Frontend { public partial class Startup { public void Configuration(IAppBuilder app) { ConfigureAuth(app); ConfigureOAuthTokenConsumption(app); } private void ConfigureOAuthTokenConsumption(IAppBuilder app) { var issuer = ConfigurationManager.AppSettings["as:Issuer"]; var audienceId = ConfigurationManager.AppSettings["as:AudienceId"]; var audienceSecret = TextEncodings.Base64Url.Decode(ConfigurationManager.AppSettings["as:AudienceSecret"]); // Api controllers with an [Authorize] attribute will be validated with JWT app.UseJwtBearerAuthentication(new JwtBearerAuthenticationOptions { AuthenticationMode = AuthenticationMode.Active, AllowedAudiences = new[] { audienceId }, IssuerSecurityTokenProviders = new IIssuerSecurityTokenProvider[] { new SymmetricKeyIssuerSecurityTokenProvider(issuer, audienceSecret) } }); } } }
您会注意到我将发行者,audienceId和audienceSecret放在我的Web.config文件中。 (这些值应与资源服务器上的值匹配)。 此外,您可能希望确保运行更新的System.IdentityModel.Tokens.Jwt:
Update-package System.IdentityModel.Tokens.Jwt
使用这些设置,您可以使用[Authorize]属性修饰控制器Action并播放球。
玩球当然意味着从你的javascript到你的受保护的控制器动作你的请求:
//assuming you placed the token in a sessionStorage variable called tokenKey when it came back from your Authorization Server var token = sessionStorage.getItem(tokenKey); var headers = {}; if (token) { headers.Authorization = 'Bearer ' + token; } $.ajax({ type: 'GET', url: 'CONTROLLER/ACTION', headers: headers }).done(function (data) { self.result(data); }).fail(showError);
UPDATE顺便说一下,如果你想在你的web.config文件中添加这些值,就像我上面那样检索它们; 只需在AppSettings下添加它们:
…当然用你自己的“价值”取而代之
我不知道你是否解决了这个问题,但我遇到了类似的问题,并决定使用FormsAuthentication存储令牌,我可以加密令牌,并在每次请求时将cookie传回,然后我可以将其解密为获取JWT,然后从中获取角色/声明,然后使用这些角色创建和Identity Principal,这将允许我使用[Authorize(Role =“blah,blah”)]来装饰我的控制器方法。
以下是一些示例代码。
从登录后的api获取JSON Web令牌后,您可以使用以下内容:
var returnedToken = (TokenResponse)result.ReturnedObject; var ticket = new FormsAuthenticationTicket(1, model.Email, DateTime.Now, ConvertUnitToDateTime(returnedToken.expires_in), true, returnedToken.access_token); string encryptedTicket = FormsAuthentication.Encrypt(ticket); var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket); cookie.HttpOnly = true; Response.Cookies.Add(cookie)
我有一些自己创建的类和方法,但它会让你大致了解在FormsAuthentication cookie中存储JWT访问令牌以及到期日期。
然后cookie随每个请求一起传递,在Global.asax文件中,您可以使用一种方法来validation请求:
protected void Application_AuthenticateRequest(Object sender, EventArgs e) { HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName]; if (authCookie != null) { //Extract the forms authentication cookie FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value); JwtSecurityToken jwTok = TokenHelper.GetJWTokenFromCookie(authCookie); // Create the IIdentity instance IIdentity id = new FormsIdentity(authTicket); // Create the IPrinciple instance IPrincipal principal = new GenericPrincipal(id, TokenHelper.GetRolesFromToken(jwTok).ToArray()); // Set the context user Context.User = principal; } }
因此,您可以解密cookie以获取JWT访问令牌,然后使用Microsoft的System.IdentityModel.Tokens.Jwt库进行解码,然后获取这些角色和ID,并为创建您的用户生成主体和身份具有角色的用户。
然后可以针对[Authorize]属性validation这些角色。
- 为ASP.NET身份配置Unity DI
- InvalidOperationException:无法为“Role”创建DbSet,因为此类型未包含在上下文的模型中
- 基于令牌的webapi实现以保护端点
- ASP.NET Core Identity不会注入UserManager
- User.IsInRole在ASP.NET Core中没有返回任何内容(已实现存储库模式)
- CreateUserIdenityAsync返回自定义IdentityUser的“UserId not found”exception
- 为什么在使用带有ASP.Net Identity的ApplicationCookie之前调用SignOut(DefaultAuthenticationTypes.ExternalCookie)?
- 如何在ASP.NET IDENTITY中添加自定义表?
- mvc 5检查用户角色