我如何在cookie中保存令牌(how I can save the token in the cookie)

   IT问题网   2021-02-20 00:00:00

问 题

嗨,

我正在创建web服务身份验证。当用户使用用户名和密码登录时。 web服务器将令牌提供给客户端以使用该令牌请求web服务。我想使用cookie来保存令牌,以便让web服务验证客户端的令牌是否正确。

以下是我的代码。如果可以,请给我示例代码。



 使用系统; 
使用 system.collections.generic;
使用 system.linq;
使用 system.web;
使用 system.web.services;
使用 system.web.services.protocols;
使用 system.text;

命名空间 authwebapplication
{
使用系统。 security.cryptography;

/// summary
/// webservice1的摘要说明
/// / summary
[webservice(namespace = " http://tempuri.org/")]
[webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
[system.componentmodel.toolboxitem( false )]
// 允许从脚本调用此web服务,使用asp .net ajax,取消注释以下行。
// [system.web.script.services.scriptservice]
public class webservice1:system.web.services.webservice
{
public webservice1()
{

// 如果使用设计的组件,则取消注释以下行
// 的initializecomponent();
}
// public authheader soapauthentication;

[webmethod(description = " 使用soap标头演示简单web服务身份验证的示例web方法" )]
public string samplewebmethod( string 用户名,字符串密码)
{

if (用户名== " demo"amp;amp; password == " 123"
{

string token = guid.newguid()。tostring();

httpruntime.cache.add(令牌,用户名, null

system.web.caching.cache。 noabsoluteexpiration,

timespan.fromminutes( 60 ),

system.web.caching.cacheitempriority.notremovable,

null );

return token + " 是一个访问web方法的经过身份验证的用户";
// 返回用户名+"是经过身份验证的用户访问web方法";
}
其他
{
返回 " 拒绝访问" +用户名;
}

}

}


}

解决方案

您可以使用response对象将内容存储在cookie中。

 response.cookies ["  userinfo"] [" 用户名"] = "  demo"; 
response.cookies [" userinfo"] [" 密码"] = " 123 ";
response.cookies [" userinfo"]。expires = datetime.now.adddays( 1 );

  string  userdata = "  123"; 


  //  创建cookie身份验证票证。 
ticket = new formsauthenticationticket(
1 // 版本
测试, // 用户名
datetime.now, // 发布时间
datetime.now.adddays( 3 ), // 每小时到期
false / / 不要保留cookie
userdata // 用户数据
);
// 加密票证
string cookiestr = formsauthentication.encrypt(ticket);

// 将cookie发送到客户端
httpcontext。 current.response.cookies [" testdata"]。value = cookiestr;
httpcontext.current.response.cookies [" testdata"]。path = " /";
httpcontext.current.response.cookies [" testdata"]。expires = datetime.now .addmonths( 3 );

标签:何在保存保留留存令牌



分享:

  • 微信
  • QQ好友
  • QQ空间
  • 新浪微博


热门推荐

C#.net中的ModalPopupExtender(ModalPopupExtender in C#.net)

problem while i am clicking the modal popup extender wind ...

需要帮助来更改密码表格(need help to change password form)

problem hi everyone, i am still newbie in vb. ...

CMap或CMapPtrToPtr(CMap or CMapPtrToPtr)

problem hi, i want store the objects in cmap ...

如何创建新的任务栏(how to create new task bar)

problem hi, i want to create new taskbar,can any one hel ...

在C#.net中旋转PDF文件(Rotate PDF File in C#.net)

problem hi , how to rotate pdf file in c#.net ...

在C#.Net中旋转图像文件(Rotate Image Files in C#.Net)

problem hi, how to rotate image files(jpeg,ti ...

如何交换字符串中的字符(how to swap characters in a string)

problem hi all. i''m new in .net and i want t ...

如何在SHAREPOINT 2010中集成twitter(How to integrate twitter in SHAREPOINT 2010)

problem hi to all, this is siddiqali working ...

检查应用程序是否正在运行(Check If Application Is Running)

problem is there a way to run a loop that wou ...

如何使用radiobuttonlist?(how to use radiobuttonlist)

problem i want to write select query for selectedindexcha ...

如何计算日期差异和数字差异(How to Calculate Date Diff And Num Diff)

problem glnotranscationdate deposit withdraw transid loan ...

ie6浏览器问题在我的网页上(ie6 browser issue in my web page)

problem my webpage displaying 1 inleft bottom corner only ...

无法在表单上使用控件。(Unable to use controls on the form.)

problem hi, i have created some static insta ...

如何在Javascript中使用正则表达式匹配定义的模式(How to match defined pattern using regular expression in Javascript)

problem hi, first, there is a string called: var bookpat ...

我想打印收据,但我遇到了问题(I want to print a reciept but i face a problem)

problem in this project i am using datareport ...

Visual Studio 2010 + Windows 7 + 64位计算机+ C ++ + 32位DLLS = 3个问题(Visual Studio 2010 + Windows 7 + 64-bit computer + C++ + 32 bit DLLS = 3 problems)

problem i''ve been using visual studio 2005 for many year ...

什么是Eclipse以及如何使用它?(What is about Eclipse and how to use it)

problem how to use it .and for what purpose it is used so ...

如何从数据库中检索数据?(how to retrieve data from database)

problem i have a radiobuttonlist control and i want to re ...

使用SQL Management Studio将数据插入我的数据库时执行NonQuery(ExecuteNonQuery while inserting the data into my database using SQL Management Studio)

problem hi i have a problem while inserting the data int ...

我如何将C#.Net中的拖放图像转换为ASP.Net(how do i convert Drag and drop image in C#.Net to ASP.Net)

problem hi, i saw the drag and drop image in c#.net arti ...