在我的Web应用程序中,我执行类似这样的操作来读取会话变量:
if (HttpContext.Current.Session != null && HttpContext.Current.Session["MyVariable"] != null)
{
string myVariable= (string)HttpContext.Current.Session["MyVariable"];
}
Run Code Online (Sandbox Code Playgroud)
我理解为什么重要的是要检查为什么HttpContext.Current.Session ["MyVariable"]为空(该变量可能尚未存储在会话中,或者会因各种原因重置了会话),但为什么我需要检查如果HttpContext.Current.Session是null?
我的理解是,会话是由ASP.NET自动创建的,因此HttpContext.Current.Session永远不应为null.这个假设是否正确?如果它可以为null,是否意味着我还应该在存储内容之前检查它:
if (HttpContext.Current.Session != null)
{
HttpContext.Current.Session["MyVariable"]="Test";
}
else
{
// What should be done in this case (if session is null)?
// Is it possible to force the session to be created if it doesn't exist?
}
Run Code Online (Sandbox Code Playgroud) 我需要在每个API请求中检索我需要检索的会话(或ASP.NET Web API中的任何内容)中存储一些信息.我们将有一个api IIS网站,并将通过主机头添加多个网站绑定.当任何请求进入时,例如api.xyz.com,将检查主机头并将该网站信息存储在会话中,该会话将在调用数据库时在每个后续api请求中使用.
我知道ASP.NET Web API中没有对会话的支持.有没有其他方法来处理这种情况?我在哪里可以存储可以在每个后续请求中检索的信息?
谢谢.
使用FormsAuthentication我们编写这样的代码:
if (IsValidUser())
{
FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
FormsAuthentication.RedirectFromLoginPage(userName, createPersistentCookie);
}
Run Code Online (Sandbox Code Playgroud)
如何手动创建身份验证cookie而不是写入FormsAuthentication.SetAuthCookie(userName, createPersistentCookie)?
如何将登录页面中的重定向URL存储在字符串变量中而不是写入FormsAuthentication.RedirectFromLoginPage(userName, createPersistentCookie)?
在IIS 7中托管的ASP.NET WebApi中,它是否可以访问会话?它似乎Session是空的HttpContext.Current.
这两个存储全局变量有什么区别?
private static Dictionary<string, string> ConnectionStrings
{
get
{
if (HttpContext.Current.Session["ConnectionStrings"] == null)
HttpContext.Current.Session["ConnectionStrings"] = new Dictionary<string, string>();
return HttpContext.Current.Session["ConnectionStrings"] as Dictionary<string, string>;
}
}
Run Code Online (Sandbox Code Playgroud)
和
private static Dictionary<string, string> connectionStrings = new Dictionary<string, string>();
Run Code Online (Sandbox Code Playgroud)
我应该使用会话或静态变量来存储动态生成的连接字符串(长篇故事)吗?
我已经浏览了大量的 Google 文档和 SO Q/A,但没有运气。我想知道是否有人按照 Google 的建议成功地使用了 OpenId 到 OpenId Connect 的迁移。
这是我们以前的做法:
IAuthenticationResponse response = _openid.GetResponse();
if (response != null) {
//omitted for brevity
} else {
IAuthenticationRequest req = _openid.CreateRequest("https://www.google.com/accounts/o8/id");
req.AddExtension(new ClaimsRequest
{
Country = DemandLevel.Request,
Email = DemandLevel.Request,
Gender = DemandLevel.Require,
PostalCode = DemandLevel.Require,
TimeZone = DemandLevel.Require
});
req.RedirectToProvider();
}
Run Code Online (Sandbox Code Playgroud)
这是使用可追溯到几年前的 DotNetOpenAuth 版本完成的。由于 Google 已弃用 OpenId 身份验证,我们正尝试转向 OpenID Connect。这里的关键问题是:我可以以某种方式得到了OpenID标识符我的手(在形式https://www.google.com/accounts/o8/id?id=xyz使用最新版本的DotNetOpenAuth库或通过)有什么其他方法吗?
我已经尝试了最新的 DotNetOpenAuth,我可以让它工作,但它给了我一个新的 Id(这是预期的)。我还通过使用这个 URL(为了可读性而换行)尝试了 Javascript 方式:
https://accounts.google.com/o/oauth2/auth?
scope=openid%20profile%20email
&openid.realm=http://localhost/palkkac/
&client_id=//here is the client id I created …Run Code Online (Sandbox Code Playgroud) 我正在使用asp.net mvc 4. 在这里,我想使用自动完成扩展器,使用JQuery我想填充CityID会话中存储的所有位置的位置。
这是创建会话的函数:
public string Create(string City)
{
try
{
//HttpSessionStateBase Session["c"]=City;
//HttpContext.Current.Session["City"] = City;
System.Web.HttpContext.Current.Session["City"] = City;
long CityID = Convert.ToInt64(System.Web.HttpContext.Current.Session["City"].ToString());
return City;
}
catch (Exception ex)
{
throw (ex);
}
}
Run Code Online (Sandbox Code Playgroud)
当用户从 City 中选择任何城市时调用此函数dropdownlist。
我JQuery对自动完成扩展程序的要求是:
<script type="text/javascript">
var url = '@Url.RouteUrl("DefaultApi", new { httproute = "", controller = "ProductApi" })';
$('#txtLocation').autocomplete({
source: function (request, response) {
alert(url);
$.ajax({
url: url,
data: { query: request.term },
dataType: …Run Code Online (Sandbox Code Playgroud) 我正在创建一些需要会话感知的 WEB API 2 控制器。我以前通过添加来做到这一点
/// <summary>
/// Application_s the post authorize request.
/// </summary>
protected void Application_PostAuthorizeRequest()
{
HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}
Run Code Online (Sandbox Code Playgroud)
但是,我们在站点的其他业务关键部分的解决方案中有 API 控制器,这些控制器已经过高度优化,返回大约 500 毫秒的响应,如果打开它,它会持续长达 2 秒。这些控制器不需要会话感知。
我们只需要提供会话访问权限的某些控制器,我已经阅读了这篇文章http://www.codeproject.com/Tips/513522/Providing-session-state-in-ASP-NET-WebAPI并且正在考虑是否可以添加具有会话意识的不同路由,但在映射路由时没有RouteHandler属性。
有没有人有任何想法?
我需要在Web API项目中的Session Data中存储一个大对象.然后我需要,客户端上的一些HTML小部件想要在Web API的会话中访问该数据,并使用它做不同的事情,比如绘制数据的图表小部件,列出数据的HTML表.我不需要为每个客户端窗口小部件重新计算大对象,而是需要将其计算为ONCE并将其存储在会话数据中,以便在同一会话中的客户端上运行的任何窗口小部件都可以在会话中的不同时间访问该数据而不必等待服务器重新计算数据.
每个小部件都应使用不同的GET请求URL访问Web API服务器项目.
我很感激帮助.我可能已经对如何做到这一点进行了高级概述,但我可以使用一些指导.
在asp.net mvc应用程序中,我想使用rest webservice返回与作为参数传递的会话ID相关联的用户名.
如何获取登录用户的sessionID?
我是否需要在登录方法中手动设置会话ID?
另外,如何使用会话ID获取用户信息?
任何帮助将不胜感激.谢谢 !
帐户控制器中的登录方法:
[HttpPost]
public ActionResult LogIn(UserLoginView ULV, string returnUrl)
{
if (ModelState.IsValid)
{
UserManager UM = new UserManager();
string password = UM.GetUserPassword(ULV.EmailID);
if (string.IsNullOrEmpty(password))
ModelState.AddModelError("", "*The Email-ID or Password provided is incorrect");
else
{
if (ULV.Password.Equals(password))
{
FormsAuthentication.SetAuthCookie(ULV.EmailID, false);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "*The Password provided is incorrect");
}
}
}
return View();
}
Run Code Online (Sandbox Code Playgroud)
用户控制器中的Web服务方法:
public class UserController : ApiController
{ …Run Code Online (Sandbox Code Playgroud)