如何从数据库传递用户详细信息? [英] How to pass user details from the database?
本文介绍了如何从数据库传递用户详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的,
我正在写信寻求帮助,我如何构建一个允许我使用来自db而不是用代码编写。
命名空间 API
{
< span class =code-keyword> public class DummyPrincipalProvider:IProvidePrincipal
{
private const string 用户名= ###;
private const string 密码= ####;
public IPrincipal CreatePrincipal( string username,字符串密码)
{
如果(用户名!=用户名||密码!=密码)
{
返回 null ;
// Page.ClientScript.RegisterStartupScript(GetType(),done,alert('your message) ');,true);
}
var identity = new GenericIdentity(用户名);
IPrincipal principal = new GenericPrincipal(identity, new [] { trial});
return principal;
}
}
}
namespace API
{
使用系统;
使用 System.Collections.Generic;
public partial class api_login
{
public string ID {获得跨度>; set ; }
public string username { get 跨度>; set ; }
public string password { get 跨度>; set ; }
public string description { get 跨度>; set ; }
public string role { get 跨度>; set ; }
}
}
更新代码
命名空间 API
{
public class BasicAuthMessageHandler:DelegatingHandler
{
private const string BasicAuthResponseHeader = WWW验证 跨度>;
private const string BasicAuthResponseHeaderValue = 基本;
public IProvidePrincipal PrincipalProvider { get ; set ; }
protected override System.Threading.Tasks.Task< HttpResponseMessage> SendAsync(
HttpRequestMessage请求,
CancellationToken cancellationToken)
{
AuthenticationHeaderValue authValue = request.Headers.Authorization;
if (authValue!= null &&!String.IsNullOrWhiteSpace(authValue.Parameter ))
{
api_login parsedCredentials = ParseAuthorizationHeader(authValue.Parameter);
if (parsedCredentials!= null )
{
Thread。 CurrentPrincipal = PrincipalProvider
.CreatePrincipal(parsedCredentials.username,parsedCredentials.password);
}
}
返回 base .SendAsync(request,cancellationToken) )
.ContinueWith(task = >
{
var response = task.Result;
if (response.StatusCode == HttpStatusCode.Unauthorized
&&!response.Headers.Contains(BasicAuthResponseHeader ))
{
response.Headers.Add(BasicAuthResponseHeader
,BasicAuthResponseHeaderValue);
}
r eturn 响应;
});
}
private api_login ParseAuthorizationHeader( string authHeader)
{
string [] credentials = Encoding.ASCII.GetString(转换
.FromBase64String(authHeader))
.Split(
new [] {' :'});
if (credentials.Length!= 2 || string .IsNullOrEmpty(凭证[ 0 ])
|| string .IsNullOrEmpty(凭证[ 1 ]))
return 空跨度>;
return new api_login()
{
username = credentials [ 0 ],
密码=凭证[ 1 ],
};
}
}
}
非常感谢任何帮助。
非常感谢你的时间和帮助。
解决方案
看一个例子在其中 [ + ]
Dear all,
I am writing to seek help, in how do I construct a method/or class that will allow me to login using the details from the db instead of written in code.
namespace API
{
public class DummyPrincipalProvider : IProvidePrincipal
{
private const string Username = "###";
private const string Password = "####";
public IPrincipal CreatePrincipal(string username, string password)
{
if (username != Username || password != Password)
{
return null;
//Page.ClientScript.RegisterStartupScript(GetType(), "done", "alert('your message');", true);
}
var identity = new GenericIdentity(Username);
IPrincipal principal = new GenericPrincipal(identity, new[] { "trial" });
return principal;
}
}
}
namespace API
{
using System;
using System.Collections.Generic;
public partial class api_login
{
public string ID { get; set; }
public string username { get; set; }
public string password { get; set; }
public string description { get; set; }
public string role { get; set; }
}
}
Updated code
namespace API
{
public class BasicAuthMessageHandler : DelegatingHandler
{
private const string BasicAuthResponseHeader = "WWW-Authenticate";
private const string BasicAuthResponseHeaderValue = "Basic";
public IProvidePrincipal PrincipalProvider { get; set; }
protected override System.Threading.Tasks.Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
AuthenticationHeaderValue authValue = request.Headers.Authorization;
if (authValue != null && !String.IsNullOrWhiteSpace(authValue.Parameter))
{
api_login parsedCredentials = ParseAuthorizationHeader(authValue.Parameter);
if (parsedCredentials != null)
{
Thread.CurrentPrincipal = PrincipalProvider
.CreatePrincipal(parsedCredentials.username, parsedCredentials.password);
}
}
return base.SendAsync(request, cancellationToken)
.ContinueWith(task =>
{
var response = task.Result;
if (response.StatusCode == HttpStatusCode.Unauthorized
&& !response.Headers.Contains(BasicAuthResponseHeader))
{
response.Headers.Add(BasicAuthResponseHeader
, BasicAuthResponseHeaderValue);
}
return response;
});
}
private api_login ParseAuthorizationHeader(string authHeader)
{
string[] credentials = Encoding.ASCII.GetString(Convert
.FromBase64String(authHeader))
.Split(
new[] { ':' });
if (credentials.Length != 2 || string.IsNullOrEmpty(credentials[0])
|| string.IsNullOrEmpty(credentials[1]))
return null;
return new api_login()
{
username = credentials[0],
password = credentials[1],
};
}
}
}
Any help would be very much appreciated.
Many thanks for your time and help.
解决方案
Take a look at an example here[+]
这篇关于如何从数据库传递用户详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文