从 class 库到 mvc5 应用程序的基于令牌的身份验证
Token based authentication from class library to mvc5 application
我有带有自定义登录实现的 mvc5 应用程序。一旦我从用户那里获得凭据,我就会制作 post 并获取令牌来验证用户。 Owin 令牌在单独的 class 库项目中实现。
[HttpPost]
[AllowAnonymous]
public ActionResult Login(UserLoginViewModel model)
{
string baseAddress = "http://localhost:4312";
Token token = new Token();
using (var client = new HttpClient())
{
var form = new Dictionary<string, string>
{
{"grant_type", "password"},
{"username", "jignesh"},
{"password", "user123456"},
};
var tokenResponse = client.PostAsync(baseAddress + "/otoken", new FormUrlEncodedContent(form)).Result;
//var token = tokenResponse.Content.ReadAsStringAsync().Result;
token = tokenResponse.Content.ReadAsAsync<Token>(new[] { new JsonMediaTypeFormatter() }).Result;
........
}
}
我不确定如何从 mvc 应用程序调用/触发 class 库项目中的令牌实现。因为 class 库项目不是可执行项目。是否有可能在单独的 class 库中实现基于令牌的实现并在不同的应用程序(mvc 和 webapi)中使用该实现。
我的图层
UI(MVC) -> 身份验证项目(Owin class 库) -> entity framework
有什么想法吗?
对于基于令牌的身份验证,您需要使用配置您的 mvc 应用程序或 webapi 来颁发令牌。 class 库无法发布 token.The 主要原因是,您需要调用配置为获取令牌的 url,例如 http://localhost:8080/api/gettoken。
所以,要么用webapi发行token,要么用mvc app发行token。
可以,但是您需要通过某种 API 公开您的应用程序登录信息。
例如,您可以创建一个令牌服务器,负责对您的 API 和 MVC 项目(外部提供商)进行身份验证,例如 Google 或 Facebook。
这也是在您的两个应用程序(MVC 和 API)之间共享相同令牌的最佳方式。
查看this篇文章,非常清楚。
希望对您有所帮助:)
我有带有自定义登录实现的 mvc5 应用程序。一旦我从用户那里获得凭据,我就会制作 post 并获取令牌来验证用户。 Owin 令牌在单独的 class 库项目中实现。
[HttpPost]
[AllowAnonymous]
public ActionResult Login(UserLoginViewModel model)
{
string baseAddress = "http://localhost:4312";
Token token = new Token();
using (var client = new HttpClient())
{
var form = new Dictionary<string, string>
{
{"grant_type", "password"},
{"username", "jignesh"},
{"password", "user123456"},
};
var tokenResponse = client.PostAsync(baseAddress + "/otoken", new FormUrlEncodedContent(form)).Result;
//var token = tokenResponse.Content.ReadAsStringAsync().Result;
token = tokenResponse.Content.ReadAsAsync<Token>(new[] { new JsonMediaTypeFormatter() }).Result;
........
}
}
我不确定如何从 mvc 应用程序调用/触发 class 库项目中的令牌实现。因为 class 库项目不是可执行项目。是否有可能在单独的 class 库中实现基于令牌的实现并在不同的应用程序(mvc 和 webapi)中使用该实现。
我的图层
UI(MVC) -> 身份验证项目(Owin class 库) -> entity framework
有什么想法吗?
对于基于令牌的身份验证,您需要使用配置您的 mvc 应用程序或 webapi 来颁发令牌。 class 库无法发布 token.The 主要原因是,您需要调用配置为获取令牌的 url,例如 http://localhost:8080/api/gettoken。 所以,要么用webapi发行token,要么用mvc app发行token。
可以,但是您需要通过某种 API 公开您的应用程序登录信息。
例如,您可以创建一个令牌服务器,负责对您的 API 和 MVC 项目(外部提供商)进行身份验证,例如 Google 或 Facebook。
这也是在您的两个应用程序(MVC 和 API)之间共享相同令牌的最佳方式。
查看this篇文章,非常清楚。
希望对您有所帮助:)