我有ASP.NET核心应用程序与角2前端.我用曲奇饼干。但是我想把我的应用程序分成两个不同的站点-- angular2上的一个前端站点和asp.net核心上的一个后端站点。
如何使用ASP.NET核心站点的auth认证前端应用程序?在我的后端网站上有一个登录页面。如何在前端应用程序中识别未通过身份验证的应用程序,然后重定向到后端应用程序,然后获得auth cookie?我不知道我是否理解这个过程的机械原理。
发布于 2016-06-20 13:52:43
我使用了基于令牌的身份验证。我选择了以下解决方案:https://stormpath.com/blog/token-authentication-asp-net-core & https://github.com/nbarbettini/SimpleTokenProvider
发布于 2018-05-20 05:33:14
对于身份验证,我更喜欢使用cookie。
登录码
[HttpPost("login")]
[AllowAnonymous]
public async Task<HttpBaseResult> Login([FromBody]LoginDto dto)
{
var user = db.Users.Include(u=>u.UserRoles).SingleOrDefault();
var claims = new List<Claim>
{
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Name, user.UserName)
};
var roles = user.UserRoles.Select(u => u.Role);
foreach (var item in roles)
{
claims.Add(new Claim(ClaimTypes.Role, item.Name));
}
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(
new ClaimsPrincipal(identity),
new AuthenticationProperties { IsPersistent = dto.RememberMe });
// ...
}交叉域
ConfigureServices
{
options.SlidingExpiration = true;
options.Cookie.HttpOnly = false;
// Dynamically set the domain name of the prod env and dev env
options.Cookie.Domain = Configuration["CookieDomain"];
});配置
app.UseCors(builder => builder.WithOrigins("http://localhost:4200", "http://www.example.com","http://example.com")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());角码
public login(userName: string, password: string, rememberMe: boolean): Observable<HttpBaseResult> {
const url: string = `${this.url}/login`;
var data = {
UserName: userName,
Password: password,
RememberMe: rememberMe
};
return this.client.post<HttpBaseResult>(url, data, { withCredentials: true });
}https://stackoverflow.com/questions/37908824
复制相似问题