如何将令牌保存在cookie中 [英] how I can save the token in the cookie

查看:366
本文介绍了如何将令牌保存在cookie中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在创建Web服务身份验证.当用户使用用户名和密码登录时. Web服务器将令牌提供给客户端,以使用该令牌请求Web服务.我想使用cookie保存令牌,以便让Web服务验证客户端的令牌是否正确.
以下是我的代码.如果可以的话,请给我示例代码.

Hi,
I am creating the web service authentication. When the user login with user name and password. Web server will give the token to the client to request the web service with that token. I would like to use the cookie to save the token in order to let the web service to authenticate whether the client''s token is correct or not.
The following is my code. please give me example code, if you can.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Text;

namespace AuthWebApplication
{
    using System.Security.Cryptography;

    /// summary
    /// Summary description for WebService1
    /// /summary
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {
        public WebService1()
        {

            //Uncomment the following line if using designed components 
            //InitializeComponent(); 
        }
        //public AuthHeader SoapAuthentication;

        [WebMethod(Description = "A sample Web Method to demonstrate a simple web Service Authentication using SOAP Headers")]
        public string SampleWebMethod(string Username, String Password)
        {

            if (Username == "demo" && Password == "123")
            {

                string token = Guid.NewGuid().ToString();

                HttpRuntime.Cache.Add(token, Username, null,

                System.Web.Caching.Cache.NoAbsoluteExpiration,

                TimeSpan.FromMinutes(60),

                System.Web.Caching.CacheItemPriority.NotRemovable,

                null);

                return token + "is an Authenticated User to access the Web Method";
              //  return Username + " is an Authenticated User to access the Web Method";
            }
            else
            {
                return "Access Denied for " + Username;
            }

        }

}


}

推荐答案

您可以使用Response对象将内容存储在cookie中.
You can use Response object to store the content in cookie.
Response.Cookies["userInfo"]["Username"] = "demo";
Response.Cookies["userInfo"]["Password"] = "123";
Response.Cookies["userInfo"].Expires = DateTime.Now.AddDays(1);


string userData ="123";


// Create a cookie authentication ticket.
               ticket = new FormsAuthenticationTicket(
                   1,                                // version
                   Test,                             // user name
                   DateTime.Now,                     // issue time
                   DateTime.Now.AddDays(3),          // expires every hour
                   false,                            // don't persist cookie
                   userData                          // user data
                   );
               // Encrypt the ticket
               String cookieStr = FormsAuthentication.Encrypt(ticket);

               // Send the cookie to the client
               HttpContext.Current.Response.Cookies["Testdata"].Value = cookieStr;
               HttpContext.Current.Response.Cookies["Testdata"].Path = "/";
                HttpContext.Current.Response.Cookies["Testdata"].Expires   = DateTime.Now.AddMonths(3);


这篇关于如何将令牌保存在cookie中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆