我Auth0用于我的用户身份验证只允许登录用户访问Spring(Boot)RestController.此时我正在创建一个实时消息功能,用户可以使用和从Angular 2客户端(localhost:4200)发送消息到Spring服务器(localhost:8081).stompjssockjs
尝试创建Stomp客户端并启动连接时,我收到以下控制台错误:
The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:4200' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.
Run Code Online (Sandbox Code Playgroud)
在研究了这个问题之后,看起来无法同时设置选项origins =*和credentials = true.当我已经将WebSocketConfig中允许的原点设置为客户端域时,如何解决此问题?
Angular 2组件
connect() {
var socket = new SockJS('http://localhost:8081/chat');
this.stompClient = Stomp.over(socket);
this.stompClient.connect({}, function(result) {
console.log('Connected: ' + result); …Run Code Online (Sandbox Code Playgroud) 我有一个ASP.NET MVC 5 webproject(localhost:81),它使用Knockoutjs从我的WebApi 2项目(localhost:82)调用函数,以便在我启用CORS的两个项目之间进行通信.到目前为止,一切都有效,直到我尝试对WebApi实施OWIN令牌认证.
要在WebApi上使用/ token端点,我还需要在端点上启用CORS,但经过几个小时的尝试和搜索解决方案后,它仍在运行,并且api/token仍然会导致:
XMLHttpRequest cannot load http://localhost:82/token. No 'Access-Control-Allow-Origin' header is present on the requested resource.
public void Configuration(IAppBuilder app)
{
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
TokenConfig.ConfigureOAuth(app);
...
}
Run Code Online (Sandbox Code Playgroud)
TokenConfig
public static void ConfigureOAuth(IAppBuilder app)
{
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<AppUserManager>(AppUserManager.Create);
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
Run Code Online (Sandbox Code Playgroud)
AuthorizationProvider
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var appUserManager = context.OwinContext.GetUserManager<AppUserManager>(); …Run Code Online (Sandbox Code Playgroud) 使用带有ASP.NET身份的Identity Server 4添加身份验证功能后,我计划添加Google提供商,以便用户也可以使用他们的Google +帐户登录.我使用Angular作为我的前端,使用ASP.NET Web Api(Core)作为后端.
// Login client
public login(email: string, password: string): Observable<any> {
let body: any = this.encodeParams({ /* cliend_id, grant_type, username, password, scope */ });
return this.http.post("http://localhost:64023/connect/token", body, this.options)
.map((res: Response) => {
const body: any = res.json();
if (typeof body.access_token !== "undefined") {
// Set localStorage with id_token,..
}
}).catch((error: any) => { /**/ );
}
// Register Web API
[HttpPost("Create")]
[AllowAnonymous]
public async Task<IActionResult> Create([FromBody]CreateUserViewModel model)
{
var user = new ApplicationUser
{
FirstName …Run Code Online (Sandbox Code Playgroud) 我有一个深度嵌套的 javascript 对象,其中包含无限数量的孩子。每个孩子都有一个值和一个 totalValue。totalValue 应该是其所有孩子和子孩子的所有值的总和。我怎样才能使这项工作?
目前我只能使用递归函数循环整个对象:
// Recursive function
_.each(names, function(parent) {
if(parent.children.length > 0) {
recursiveFunction(parent.children);
}
});
function recursiveFunction(children){
_.each(children, function(child) {
if(child.children.length > 0) {
recursiveFunction(child.children)
}
});
};
// Deeply nested javascript object
var names = {
name: 'name-1',
value: 10,
valueTotal: 0, // should be 60 (name-1.1 + name-1.2 + name-1.2.1 + name-1.2.2 + name-1.2.2.1 + name-1.2.2.2)
children: [{
name: 'name-1.1',
value: 10,
valueTotal: 0,
children: []
}, {
name: 'name-1.2',
value: 10,
valueTotal: 0, …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用jQuery将文件发送到我的MVC控制器,但该操作仍然接收一个空的HttpPostedFileBase参数.
HTML:
<input type="file" name="file" id="file" />
<input type="submit" name="submit" id="upload" value="Submit"/>
Run Code Online (Sandbox Code Playgroud)
jQuery的:
$(function () {
$('#upload').click(function () {
var data = new FormData($('#file')[0].files[0]);
$.ajax({
url: '@Url.Action("Upload", "Home")',
type: 'POST',
data: data,
cache: false,
contentType: false,
processData: false
});
});
});
Run Code Online (Sandbox Code Playgroud)
控制器:
[HttpPost]
public virtual ActionResult Upload(HttpPostedFileBase file)
{
// file = null
}
Run Code Online (Sandbox Code Playgroud)
new FormData($('#file')[0] .files [0]):
__proto__: FormData
Run Code Online (Sandbox Code Playgroud)
$( '#文件')[0] .files [0]:
lastModified: 1445429215528
lastModifiedDate: Wed Oct 21 2015 14:06:55 GMT+0200 (Central Europe Daylight Time)
name: "Google_Chrome_logo_2011.jpg"
size: …Run Code Online (Sandbox Code Playgroud) 如何显示特定角色中所有用户的列表.
我将IdentityRole模型附加到我的View,并为其分配了"Admin"角色.到目前为止我只能得到UserId.
@model Microsoft.AspNet.Identity.EntityFramework.IdentityRole
@Html.DisplayNameFor(model => model.Name) // Shows 'Admin'
@foreach (var item in Model.Users)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserId)
</td>
</tr>
}
Run Code Online (Sandbox Code Playgroud)
一种可能的解决方案是在控制器中创建用户列表并将其附加到视图.问题是我还需要角色本身的数据.
我正在使用 FabricJS 创建全屏画布并在服务器端保存更改。在加载之前,我调整了画布和背景的大小,以便在每个页面加载时画布都适合用户窗口。
canvas.setHeight($(window).height());
canvas.setWidth($(window).width());
canvas.backgroundImage.width = $(window).width();
canvas.backgroundImage.height = $(window).height();
Run Code Online (Sandbox Code Playgroud)
问题是画布和背景得到了调整,而不是对象。它们相对于屏幕保持相同的位置并且不会改变尺寸,因此在其他屏幕尺寸上是不正确的。如何解决这个问题?