GVKun编程网logo

如何从 NextJs 访问 HttpContext.Session

12

以上就是给各位分享如何从NextJs访问HttpContext.Session,同时本文还将给你拓展.NET5HttpContext.Session不在webviewandroid上维护状态、AJAX

以上就是给各位分享如何从 NextJs 访问 HttpContext.Session,同时本文还将给你拓展.NET 5 HttpContext.Session 不在 webview android 上维护状态、AJAX:HttpContext context.Session["XX"]写入时报错、Asp.net System.Web.HttpContext.Current.Session null in global.asax、asp.net – HttpContext.Current.User!= HttpContext.User?等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何从 NextJs 访问 HttpContext.Session

如何从 NextJs 访问 HttpContext.Session

如何解决如何从 NextJs 访问 HttpContext.Session?

我使用 Nextjs 作为前端,使用 Asp.Net 核心作为后端 (API)。我使用 JWT 身份验证从 Next Js 使用 axios 访问端点。我还在后端设置会话来存储用户数据。问题是 HttpContext.Session 始终为空。以下是我使用的代码示例:

axios.post(`https://localhost:44301/Home/GetValues`,{ },{
        headers: {
            ''Authorization'': `Bearer ${token}`,''Content-Type'': ''application/json''

        },withCredentials: true
    }
)
    .then(res => {
        console.log("Values",res.data);          

    })
    .catch(err => console.log(err));

使用 Postman 可以访问 HttpContext.Session。这意味着 Nextjs 不会发送由 Asp.net 核心设置的会话 cookie。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

.NET 5 HttpContext.Session 不在 webview android 上维护状态

.NET 5 HttpContext.Session 不在 webview android 上维护状态

如何解决.NET 5 HttpContext.Session 不在 webview android 上维护状态?

我有一个在 WebView android 上使用的网站。不知何故 HttpContext.Session 在 webview android 上不起作用,而在 web 和 iOS WebView 上运行完美。

这是我的 C# 代码:

获取字符串的字节数

var byteAmount = Encoding.UTF8.GetBytes(amount);

这里我将字节设置为会话:

HttpContext.Session.Set(SessionKeyAmount,byteAmount);

在这部分,当从 android webview 请求时

var amount = HttpContext.Session.Get(SessionKeyAmount);
var amountasstring = Encoding.UTF8.GetString(amount);

amount 为空。

这是android代码的一部分:

CookieManager cookieManager = android.webkit.CookieManager.getInstance();
cookieManager.setAcceptThirdPartyCookies(webview,true);
cookieManager.setAcceptCookie(true);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

AJAX:HttpContext context.Session[

AJAX:HttpContext context.Session["XX"]写入时报错

错误:POST http://localhost:52336/CampusBattle/handlers/UserHandler.ashx 500 (Internal Server Error)

描述:

无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查 HKEY_LOCAL_MACHINE\SYstem\CurrentControlSet\Services\aspnet_state\Parameters\AllowRemoteConnection 的值,确保服务器接受远程请求。如果服务器位于本地计算机上,并且上面提到的注册表值不存在或者设置为 0,则状态服务器连接字符串必须使用“localhost”或“127.0.0.1”作为服务器名称。

解决方法:
1.此电脑(我的电脑)-右键管理-服务和应用程序-服务-ASP.NET State Service;
2.启动类型选择自动-点击启动-确定。

Asp.net System.Web.HttpContext.Current.Session null in global.asax

Asp.net System.Web.HttpContext.Current.Session null in global.asax

我有一个自定义安全主体对象,我在global.asax中为当前线程设置,一切都很好,没有问题.

但是,我只是添加一个动态图像功能,让一个页面提供图像,每当加载动态图像页面System.Web.HttpContext.Current.Session是null在global.asax,这阻止我设置安全校长是从那时开始的正常和级联问题.

通常,在用户登录时,在开始时的会话期间,global.asax中的Session仅为null,之后它始终可用于此单个异常.

当浏览器碰到原始页面中的图像时,加载动态图像页面,即

我猜这是浏览器在不发送一些凭据的情况下请求该页面的一个方面呢?

任何帮助将不胜感激.

解决方法

约翰,

我假设你正在为处理程序使用一个ashx处理程序.如果是这样,请务必从IRequiresSessionState派生例如:

public class Images : IHttpHandler,System.Web.SessionState.IRequiresSessionState
{ }

如果您不使用ashx,您可以描述动态图像页面的意思吗?

玩笑

asp.net – HttpContext.Current.User!= HttpContext.User?

asp.net – HttpContext.Current.User!= HttpContext.User?

全局asax中的HttpContext.Current.User与动作方法中的HttpContext.User不同吗?我为用户分配了一些角色,但他们似乎迷路了.

下面的代码显示了正在发生的事情.当用户登录时,两个Assert都会被点击,首先是全局的asax,然后是action方法.但是他们给出了不同的结果

首先这个:

protected void Application_AuthenticateRequest(object sender,EventArgs e)
{
    // ... omitted some code to check user is authenticated
    FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;

    string[] roles = new string[] { "admin","user" };

    HttpContext.Current.User =
        new System.Security.Principal.GenericPrincipal(identity,roles);

    Assert(HttpContext.User.IsInRole("admin"));
}

然后在我的动作方法中:

public ActionResult Index()
{
    bool isAdmin = HttpContext.User.IsInRole("admin");

    Assert(isAdmin); // this fails,isAdmin is false

    // ...
}

我使用了以下资源

This SO answer

http://csharpdotnetfreak.blogspot.com/2009/02/formsauthentication-ticket-roles-aspnet.html

解决方法

你的问题标签上写着“aspnet-mvc(3和4)”,你可以选择使用以下内容让你的生活更轻松吗?如果您使用的是VS2012中的MVC 4 Internet应用程序模板中的 Simple Membership,那么您只需开箱即用):

> WebSecurity.CreateUserAndAccount(name,password) – 创建用户
> Roles.AddUserToRole(和AddUserToRoles) – 将用户添加到角色
> Roles.IsUserInRole – 测试用户是否在角色中
> [Authorize(Roles = "admin")] – [授权]可以在整个控制器或操作上强制执行角色

createuserAndAccount的优点是也可以轻松设置UserProfile的属性,例如:

WebSecurity.createuserAndAccount(newUser.UserName,newUser.Password,new { FullName = newUser.FullName,Email = newUser.Email,Timezone = newUser.TZ });
Roles.AddUserToRoles(newUser.UserName,new[] {"admin","user"});

编辑,我意识到上面没有回答你关于.User属性等价的原始问题.

Controller中的HttpContext是一个属性:Controller.HttpContext.global.asax.cs中的HttpContext是静态类,这就是你使用HttpContext.Current的原因.他们指的是同一件事.

如果您运行以下代码,您可以看到它们显然是“相同的主体”.那么问题是你分配的角色发生了什么?

protected void Application_AuthenticateRequest(object sender,EventArgs e) {
    ...
    FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
    string[] roles = new string[] { "admin","user" };
    identity.Label = "test label";
    System.Security.Principal.GenericPrincipal ppl = new System.Security.Principal.GenericPrincipal(identity,roles);            
    HttpContext.Current.User = ppl;
... }

public ActionResult Index() {
    bool isAdmin = HttpContext.User.IsInRole("admin");
    bool isAdmin2 = System.Web.HttpContext.Current.User.IsInRole("admin");
    System.Web.Security.FormsIdentity identity = (System.Web.Security.FormsIdentity)HttpContext.User.Identity;

    // The label is carried through from Application_AuthenticateRequest to Index.
    string label = identity.Label;
}

问题是,您为.User分配了GenericPrincipal.根据RoleProvider,可以在PostAuthenticateRequest期间覆盖(例如,通过RoleManagerModule),并且(例如)将其转换为RolePrincipal.然后,这可以推迟回到数据库(再次取决于提供者)以获取角色,从而覆盖您的角色.如果您在Application_OnPostAuthenticateRequest中完成工作,那么您可能没问题.

关于如何从 NextJs 访问 HttpContext.Session的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于.NET 5 HttpContext.Session 不在 webview android 上维护状态、AJAX:HttpContext context.Session["XX"]写入时报错、Asp.net System.Web.HttpContext.Current.Session null in global.asax、asp.net – HttpContext.Current.User!= HttpContext.User?的相关信息,请在本站寻找。

本文标签: